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 ...
随机推荐
- shell 日期转换
1.字符串转换为时间戳可以这样做: date -d "2010-10-18 00:00:00" +%s 输出形如: 1287331200 其中,-d参数表示显示指定的字符串所表示的 ...
- 【DRF权限】
目录 权限的详细用法 我们都听过权限,那么权限到底是做什么的呢. 我们都有博客,或者去一些论坛,一定知道管理员这个角色, 比如我们申请博客的时候,一定要向管理员申请,也就是说管理员会有一些特殊的权利, ...
- Oracle 10g 10.2.0.1 在Oracle Linux 5.4 32Bit RAC安装手冊(一抹曦阳)
Oracle 10g 10.2.0.1 在Oracle Linux 5.4 32Bit RAC安装手冊(一抹曦阳).pdf下载地址 ,step by step http://download.csdn ...
- NOPI 锁定Excel单元格不让编辑的方法
简介:原生态纯JavaScript 100大技巧大收集---你值得拥有 http://www.cnblogs.com/xl900912/p/4223629.html 从博客园上看都的关于JS的一些常见 ...
- 1.17 Python基础知识 - 迭代器和生成器初识
可循环迭代的对象称为可迭代对象,迭代器和生成器函数是可迭代对象. 列表解析表达式:可以简单高效处理一个可迭代对象,并生成结果列表 示例代码: [ i ** 2 for i in range(10) ] ...
- C# Find() 与 FindAll()方法的使用
Find() :检索与指定匹配的第一个元素 FindAll() : 检索与指定匹配的所有元素 如:List<string> strList=new List<string&g ...
- HDU 2489 Minimal Ratio Tree(prim+DFS)
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- CentOS卸载Apache方法
https://www.kafan.cn/edu/49420412.html CentOS卸载Apache方法 首先关闭httpd服务 /etc/init.d/httpd stop 列出httpd相关 ...
- java 文件读写--转载
读文件 http://www.baeldung.com/java-read-file Java – Read from File 1. Overview In this tutorial we’ll ...
- Spring MVC handler interceptors example--转载
原文地址:http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/ Spring MVC allow you ...