B - Bill Total Value(小数点问题处理)
2017-11-24 09:22
429 查看
Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row without any spaces. Check has the format "name1price1name2price2...namenpricen", where namei (name of the i-th purchase) is a non-empty string of length not more than 10, consisting of lowercase English letters, and pricei (the price of the i-th purchase) is a non-empty string, consisting of digits and dots (decimal points). It is possible that purchases with equal names have different prices. The price of each purchase is written in the following format. If the price is an integer number of dollars then cents are not written. Otherwise, after the number of dollars a dot (decimal point) is written followed by cents in a two-digit format (if number of cents is between 1 and 9 inclusively, there is a leading zero). Also, every three digits (from less significant to the most) in dollars are separated by dot (decimal point). No extra leading zeroes are allowed. The price always starts with a digit and ends with a digit. For example: "234", "1.544", "149.431.10", "0.99" and "123.05" are valid prices, ".333", "3.33.11", "12.00", ".33", "0.1234" and "1.2" are not valid. Write a program that will find the total price of all purchases in the given bill.
Input
The only line of the input contains a non-empty string s with length not greater than 1000 — the content of the bill. It is guaranteed that the bill meets the format described above. It is guaranteed that each price in the bill is not less than one cent and not greater than 106 dollars.
Output
Print the total price exactly in the same format as prices given in the input.
Example
Input
chipsy48.32televizor12.390
Output
12.438.32
Input
a1b2c3.38
Output
6.38
Input
aa0.01t0.03
Output
0.04
题意:算总和,如果这个数字小数点后面有三位的话,这三位就是整数,如果小数点后面只有两位的话,这两位是小数,不会出现一位的情况。在计算的过程中把所有小数加起来,把所有整数加起来,要及得最后在输出之前看一下小数加到需要进位的情况。字符串输入,每遇到一个数就分别把他的 整数部分和小数部分加起来。
#include<stdio.h> #include<stdlib.h> #include<string.h> char s[10005]; int main() { int dol=0, cen=0; scanf("%s",s); char num[10005]; int len = strlen(s); int i,j; for(i=0;i<len;i++) { if(s[i]>='a'&&s[i]<='z') continue; memset(num,0,sizeof(num)); for(j=i;j<len;j++) { if((s[j]<='z'&&s[j]>='a')||j>=len) { num[j] = '\0'; break; } num[j-i] = s[j]; } i = j-1; int dolt=0,cent = 0; int len1 = strlen(num); if(num[len1 - 3]=='.')///有小数部分的时候 { cent += (int)(num[len1-1]-'0'); cent += (int)(num[len1-2]-'0')*10; cen += cent; for(int k=0;k<len1-3;k++) { if(num[k]=='.') continue; dolt=dolt*10+(int)(num[k]-'0'); } dol += dolt; } else///只有整数部分的时候 { for(int k=0;k<len1;k++) { if(num[k] == '.') continue; dolt=dolt*10+(int)(num[k]-'0'); } dol += dolt; } } ///输出 dol += cen/100;///分数加到需要进位的时候; cen %= 100; int re1[1100],ct1 = 0; memset(re1,0,sizeof(re1)); if(dol == 0) printf("0"); else { while(dol>0) { re1[ct1++] = dol%10; dol /= 10; } } int dian = 0; for(int k = ct1-1;k>=0;k--)///数是倒着存的 { dian++; printf("%d",re1[k]); if((ct1-dian)%3==0&&(ct1-dian)!=0) { printf("."); } } if(cen!=0) { printf("."); printf("%02d\n",cen); } return 0; }
相关文章推荐
- CodeForces--TechnoCup--2016.10.15--ProblemB--Bill Total Value(字符串处理)
- Technocup 2017 - Elimination Round 1 (Rated for Div. 2) B. Bill Total Value 表达式处理、sstream
- JAVA数字千分位和小数点的现实(处理金额问题)
- 【21.67%】【codeforces 727B】Bill Total Value
- 项目问题反馈:对后台查询的结果列表进行处理:计算平均值,改变小数点位数
- java.lang.IllegalArgumentException: Page directive: invalid value for import 问题处理
- Java处理Oracle数据库小数点前面的0丢失问题
- java.lang.IllegalArgumentException: Page directive: invalid value for import 问题处理
- code force B. Bill Total Value
- 将int字段改为decimal类型后出现1264 Out of range value的问题分析和处理之二
- ADC0809使用中的小问题----小数点后保留两位的处理方法
- 例4.10 POJ3525/LA3890离海最远的点 半平面交 + 二分法 + double小数点后有效位数处理方式/printf与g++、c++的问题
- 将int字段改为decimal类型时出现1264 Out of range value的问题分析和处理
- JAVA数字千分位和小数点的现实(处理金额问题)
- A potentially dangerous Request.Form value was detected from the client问题处理
- Oracle小数点保留的问题的的4个处理方案
- 法语系统小数点的处理。Convert.ToDouble(string str)的问题
- java及javascript处理金额小数点问题
- ORACLE中小数点问题的处理
- mysql的utf8mb4编码以及java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' 问题的处理