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. API(Application Programming Interface,应用程序编程接口)

    API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码 ...

  2. canvas:画布

    画布有默认宽度,如果要自己设置宽带要写在属性上 列: <canvas id = "myCanvas" width = "600" height = &qu ...

  3. JavaScript学习总结(10)——实用JS代码大全

    事件源对象  event.srcElement.tagName  event.srcElement.type 捕获释放  event.srcElement.setCapture();   event. ...

  4. AES与RAS结合加解密方案

    import java.io.IOException; import java.security.InvalidKeyException; import java.security.KeyFactor ...

  5. new不抛出异常nothrow与new_handler

    可以看这里: http://blog.csdn.net/huyiyang2010/article/details/5984987 现在的new是会抛出异常的,bad::alloc 如果不想抛出异常两种 ...

  6. FileChannel的深入理解

    一,官方描写叙述 一个读,写,映射,操作文件的通道. 文件通道有能够被查询和改动的一个当前位置.文件本身包括了一个可悲读写的变长字节序列,而且它的当前的size会被查询.当被写入的字节超过当前文件的大 ...

  7. 3.第一个Node.js程序:Hello World!

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html 以下是我们的第一个Node.js程序: console.log("Hello Wor ...

  8. Maven中央仓库信息速查

    http://maven.outofmemory.cn/

  9. enq: TX - row lock contention故障处理一则

    一个非常easy的问题,之所以让我对这个问题进行总结.一是由于没我想象的简单,在处理的过程中遇到了一些磕磕碰碰,甚至绕了一些弯路.二是引发了我对故障处理时的一些思考. 6月19日,下午5点左右.数据库 ...

  10. 发送 email 过程

    发送 email 过程 SMTP基本命令集: 命令 描述 ---------- HELO 向服务器标识用户身份发送者能欺骗,说谎,但一般情况下服务器都能检测到. MAIL 初始化邮件传输 mail f ...