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. IOS打包发布APP的所有详细流程

    其他一些不错的参考:点击打开链接 一.申请苹果开发者账号 首先需要申请苹果开发者账号才能在APP store 里发布应用. 开发者账号分为:(1)个人开发者账号   (2)企业开发者账号   主要的区 ...

  2. codeforces 688D

    题意: 给你n和k,表示有n个数,c1到cn,然后让你求一个数x,可以告诉你x%ci的值,问你是否可以唯一确定一个x%k的值 题解: 反证: 假设有两个x1,x2同时是解,则对于所有ci,x1%ci= ...

  3. 03009_SQL注入问题

    1.注入问题 (1)假设有登录案例SQL语句如下: SELECT * FROM 用户表 WHERE NAME = 用户输入的用户名 AND PASSWORD = 用户输的密码; (2)此时,当用户输入 ...

  4. ubuntu-smb共享文件创建

    如何在计算机上实现资源共享                         --在本地用户目录下,创建一个smb文件夹                         --右击,选择share opt ...

  5. Linux 内存管理与系统架构设计

    Linux 提供各种模式(比如,消息队列),但是最著名的是 POSIX 共享内存(shmem,shared memory). Linux provides a variety of schemes ( ...

  6. Impala架构

    Impala是Cloudera在受到Google的Dremel启发下开发的实时交互SQL大数据查询工具,Impala没有再使用缓慢的 Hive+MapReduce批处理,而是通过使用与商用并行关系数据 ...

  7. golang panic and recover

    panic 是一个内置函数,当一个函数 F 调用 panic,F 的执行就会停止,F 中 deferred 函数调用会被执行,然后 F 返回控制到它的调用者.这个过程会沿着调用栈执行下去,直到当前 g ...

  8. JavaScript 进度条重复加载

    <!DOCTYPE HTML> <html> <head> <meta charset ="utf-8"> <title> ...

  9. ArcGIS 点要素新增点

    IFeatureLayer layer = FrmMain.m_mapControl.get_Layer(0) as IFeatureLayer; IFeatureClass featureClass ...

  10. Activity启动模式的深入分析

    网上关于Activity启动模式的文章许多.可是看起来都千篇一律,看完之后我们都能理解这4种启动模式.只是官方api对singleTask这个启动模式解释有些争议,导致我事实上并没有真正理解这几种模式 ...