ZOJ 2476 Total Amount 字符串
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1476
题目大意:
给你n串数字组成的字符串,要求输出他们相加的和。
如:n= 2
输入$1,123.45和$2,890.23要求输出$4,013.68
思路:
先存入字符数组,然后在转化为double,然后在用sprintf存进字符数组,然后判断是否要输出','输出即可
#include<cstdio>
#include<cstring>
const int MAXN=50;
char a[MAXN];
int main()
{
int n;
while(~scanf("%d",&n),n)
{
double ans=0;
double temp;
for(int i=0;i<n;i++)
{
temp=0;
scanf("%s",a);
int len=strlen(a);
bool point=false;
for(int j=1;j<len;j++)
{
if(a[j]==',')
continue;
if(a[j]=='.')
{
point=true;
continue;
} if(point)
{
if(j==len-2)
temp+=(a[j]-'0')*0.1+(a[j+1]-'0')*0.01;
else if(j==len-1)
temp+=(a[j]-'0')*0.1;
break;
}
else
temp=temp*10+a[j]-'0';
}
ans+=temp;
}
bool print_commas[MAXN]={0};
char res[MAXN];
sprintf(res,"%.2lf",ans); int id=strchr(res,'.')-res; for(int cnt=1;id>=0;id--)
{
if(cnt==3)
{
print_commas[id]=true;
cnt=1;
continue;
}
cnt++;
}
int len=strlen(res);
printf("$");
for(int i=0;i<len;i++)
{
if( i!=0 &&print_commas[i+1]==true) //忘了i!=0的判断了- -|||
printf(",");
printf("%c",res[i]);
}
printf("\n");
}
return 0;
}
ZOJ 2476 Total Amount 字符串的更多相关文章
- ZOJ 2476 Total Amount 字符串模拟
- Total Amount Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- ZOJ 2476 Total Amount
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2476 Time Limit: 2 Seconds ...
- hdu 2476 (string painter) ( 字符串刷子 区间DP)
String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ZOJ 3490 String Successor 字符串处理
一道模拟题,来模拟进位 暴力的从右往左扫描,按规则求后继就好了.除了Sample已给出的,还有一些需要注意的地方: 9的后继是10,而不是00: (z)的后继是(aa),而不是a(a): 输入虽然最长 ...
- zoj 1151 Word Reversal(字符串操作模拟)
题目连接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1151 题目描述: For each list of words ...
- zoj 2860 四边形优化dp
Breaking Strings Time Limit: 2 Seconds Memory Limit: 65536 KB A certain string-processing lan ...
- Codeforces Round #367 (Div. 2) A B C 暴力 二分 dp(字符串的反转)
A. Beru-taxi time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 1079. Total Sales of Supply Chain (25)
时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...
- 1079. Total Sales of Supply Chain (25) -记录层的BFS改进
题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...
随机推荐
- javafx tabPane
public class EffectTest extends Application { @Override public void start(Stage primaryStage) { prim ...
- 使用Vue动态生成form表单
form-create 表单生成器 具有数据收集.校验和提交功能的表单生成器,支持双向数据绑定和事件扩展,组件包含有复选框.单选框.输入框.下拉选择框等表单元素以及省市区三级联动,时间选择,日期选择, ...
- Java证书通信
一.概念介绍: 加密是将数据资料加密,使得非法用户即使取得加密过的资料,也无法获取正确的资料内容,所以数据加密可以保护数据,防止监听攻击.其重点在于数据的安全性.身份认证是用来判断某个身份的真实性 ...
- Java生产者与消费者(上)
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 生产与消费者模式,是编程中最常用的模式之一,在多线程中应用比较明显.个人理解:在自助餐厅,厨师在不断 ...
- 8.Maven之(八)约定优于配置
转自:“https://blog.csdn.net/qq_25460531/article/details/79423961” maven的配置文件看似很复杂,其实只需要根据项目的实际背景,设置个别的 ...
- 使用NPOI时写的几个辅助方法
简介:包含:获取单元格合并信息GetMergedCellAddress.获取引用单元格字符串ConvertAddressToString.获取单元格字符串格式内容CellValueToString p ...
- canvas:动态时钟
此时针是以画布的中心为圆心: ctx.translate(width/2,width/2); 此函数是将画布的原点移到(width/2,width/2) 数字的位置我们利用了三角函数的原理 x=rco ...
- session应用二
从session中获取mapper对象,利用mapper对象进行增删改查 Date now = new Date(); SqlSession session = this.yangchebaoDbMa ...
- oracle数据库spfile
http://blog.chinaunix.net/uid-8996530-id-3195808.html http://www.cnblogs.com/HondaHsu/p/4885318.html ...
- Android Studio中怎样引用图片资源
随着不断接触Android Studio,越来越发现和Eclipse有着巨大的差别. 不管是表面的目录结构,还是内在对各种资源的管理. 本篇就来聊聊Android Studio中怎样来显示图片. 以及 ...