ZOJ 2476 Total Amount 字符串
来源:互联网 发布:手机解压rar软件 编辑:程序博客网 时间:2024/05/17 05:03
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;}elsetemp=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;}
1 0
- ZOJ 2476 Total Amount 字符串
- ZOJ 2476 Total Amount (字符串处理)
- zoj 2476 Total Amount
- zoj 2476 Total Amount
- ZOJ 2476 Total Amount
- Total Amount zoj 2476
- ZOJ 2476 Total Amount
- zoj 2476 Total Amount
- zoj 2476 Total Amount
- B-Total Amount (ZOJ 2476)
- zoj 2476 Total Amount(水~)
- ZOJ Problem Set - 2476 Total Amount
- ZOJ 2476 Total Amount A的好辛苦
- zoj 2476 Total Amount(模拟题,细节处理特别多)
- zoj_2476 Total Amount
- zoj 3023 Equal Total Scores
- ZOJ 3023 Equal Total Scores
- find the total amount of memory consumed (including AWE) by the buffer pool
- 如何检查电脑驱动程序
- linux下java环境变量设置
- OpenStack Havana版发布
- 几个开源流媒体服务器
- Android Studio如何发布APK
- ZOJ 2476 Total Amount 字符串
- Category 类别 知识点整理
- 产业相关名词解释
- My new English
- java排序----鸡尾酒排序
- 排序、冒泡、二叉树以及一致性hash算法
- JAVA SE----程序开发的一些建议以及eclipse调试说明
- Mysql
- 浅析Android 蜂巢中左下角三个导航键的实现源码