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 字符串的更多相关文章

  1. ZOJ 2476 Total Amount 字符串模拟

    - Total Amount Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit ...

  2. ZOJ 2476 Total Amount

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2476 Time Limit: 2 Seconds         ...

  3. hdu 2476 (string painter) ( 字符串刷子 区间DP)

    String painter Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. ZOJ 3490 String Successor 字符串处理

    一道模拟题,来模拟进位 暴力的从右往左扫描,按规则求后继就好了.除了Sample已给出的,还有一些需要注意的地方: 9的后继是10,而不是00: (z)的后继是(aa),而不是a(a): 输入虽然最长 ...

  5. zoj 1151 Word Reversal(字符串操作模拟)

    题目连接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1151 题目描述: For each list of words ...

  6. zoj 2860 四边形优化dp

    Breaking Strings Time Limit: 2 Seconds        Memory Limit: 65536 KB A certain string-processing lan ...

  7. 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 ...

  8. 1079. Total Sales of Supply Chain (25)

    时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...

  9. 1079. Total Sales of Supply Chain (25) -记录层的BFS改进

    题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...

随机推荐

  1. js闭包中注意文字总结

    //闭包注意的点 //闭包中使用外部变量不是复制而是引用 //闭包可以节省传递参数问题 //在循环中使用闭包

  2. css3--根据数据加载显示的一个动画

    css: .circle { width: 200px; height: 200px; position: absolute; border-radius: 50%; background: #0cc ...

  3. BZOJ1814: Ural 1519 Formula 1(插头Dp)

    Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...

  4. struct数组初始化

    const int MAXN=100; struct A { int a,b; }; struct A arr[100];//此时编译通过 struct A arr[MAXN];//此时编译不通过,原 ...

  5. ubuntu搭建交叉编译环境makeinfo: command not found

    解决办法:sudo apt-get install texinfo

  6. 一起talk C栗子吧(第九十 三回:C语言实例--进程间通信之临界资源)

    各位看官们.大家好,前面章回中咱们说的是使用信号和管道进行进程间通信的样例.这一回咱们说的样例是:进程间通信之临界资源.闲话休提,言归正转.让我们一起talk C栗子吧! 我们首先介绍一下,什么是临界 ...

  7. DBLINK做系统集

    过度使用DBLINK做系统集成会带来的问题 过度使用DBLINK做系统集成会带来很多问题,问题主要由以下几点: 1. 大量消耗数据库资源: 本地系统每通过DBLINK链接远端系统一次,都会生成一个本地 ...

  8. Binary Search Algorithm

    二分查找代码: //============================================================================ // Name : Bin ...

  9. vue使用jsonp

    axios不支持jsonp,所以需使用其他插件:vue-jsonp npm i vue-jsonp -S 然后在 src/main.js : import Vue from 'vue' import ...

  10. js日期常用方法

    js获取日期时间格式 export function formatDateTime(timeStamp) { var date = new Date(); date.setTime(timeStamp ...