zoj 2476 Total Amount
2016-06-05 22:00
288 查看
Given a list of monetary amounts in a standard format, please calculate the total amount.
We define the format as follows:
1. The amount starts with '$'.
2. The amount could have a leading '0' if and only if it is less then 1.
3. The amount ends with a decimal point and exactly 2 following digits.
4. The digits to the left of the decimal point are separated into groups of three by commas (a group of one or two digits may appear on the left).
Input
The input consists of multiple tests. The first line of each test contains an integer N (1 <= N <= 10000) which indicates the number of amounts. The next N lines contain N amounts. All amounts and the total amount are between $0.00 and $20,000,000.00, inclusive.
N=0 denotes the end of input.
Output
For each input test, output the total amount.
Sample Input
2
$1,234,567.89
$9,876,543.21
3
$0.01
$0.10
$1.00
0
Sample Output
$11,111,111.10
$1.11
题意:就是求和
思路:直接模拟求和累加即可
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
#include <stack>
#include <map>
#include <vector>
using namespace std;
char ans[50005];
int main() {
int t;
while(cin >> t && t) {
memset(ans,0,sizeof(ans));
int cnt = 0;
while(t--) {
getchar();
char gra[105];
char shu[105];
memset(gra,0,sizeof(gra));
memset(shu,0,sizeof(shu));
cin >> gra;
int len = strlen(gra);
int g = 0;
for(int i = len-1; i > 0; i--) {
shu[g++] = gra[i];//把输入的数反过来
}
int lena = strlen(ans);
int k = 0;
int h = max(lena,g);
for(int i = 0; i<h; i++) {
if(shu[i] == ','|| ans[i] == ',') {
ans[i] = ',';
continue;
}
if(shu[i] == '.'|| ans[i] == '.') {
ans[i] = '.';
continue;
}
if(ans[i] != 0) {
k += ans[i]-'0';
}
if(shu[i] != 0) {
k += shu[i]-'0';
}
if(k >= 10) {
ans[i] = k-10+'0';
k = 1;//进位
} else {
ans[i] = k+'0';
k = 0;//不进位
}
}
if(k == 1) {//如果超出三个的话加上逗号
if(h-4 >= 0 && (ans[h-4] == ','||ans[h-4]=='.')){
ans[h++] = ',';
}
ans[h++] = k+'0';
}
}
printf("$");
cnt = strlen(ans);
for(int i = cnt-1; i >= 0; i--) {
printf("%c",ans[i]);
}
printf("\n");
}
return 0;
}
很无语,简单题,写了很长时间,各种坑,各种无奈,最近状态又有点不好
Given a list of monetary amounts in a standard format, please calculate the total amount.
We define the format as follows:
1. The amount starts with '$'.
2. The amount could have a leading '0' if and only if it is less then 1.
3. The amount ends with a decimal point and exactly 2 following digits.
4. The digits to the left of the decimal point are separated into groups of three by commas (a group of one or two digits may appear on the left).
Input
The input consists of multiple tests. The first line of each test contains an integer N (1 <= N <= 10000) which indicates the number of amounts. The next N lines contain N amounts. All amounts and the total amount are between $0.00 and $20,000,000.00, inclusive.
N=0 denotes the end of input.
Output
For each input test, output the total amount.
Sample Input
2
$1,234,567.89
$9,876,543.21
3
$0.01
$0.10
$1.00
0
Sample Output
$11,111,111.10
$1.11
题意:就是求和
思路:直接模拟求和累加即可
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
#include <stack>
#include <map>
#include <vector>
using namespace std;
char ans[50005];
int main() {
int t;
while(cin >> t && t) {
memset(ans,0,sizeof(ans));
int cnt = 0;
while(t--) {
getchar();
char gra[105];
char shu[105];
memset(gra,0,sizeof(gra));
memset(shu,0,sizeof(shu));
cin >> gra;
int len = strlen(gra);
int g = 0;
for(int i = len-1; i > 0; i--) {
shu[g++] = gra[i];//把输入的数反过来
}
int lena = strlen(ans);
int k = 0;
int h = max(lena,g);
for(int i = 0; i<h; i++) {
if(shu[i] == ','|| ans[i] == ',') {
ans[i] = ',';
continue;
}
if(shu[i] == '.'|| ans[i] == '.') {
ans[i] = '.';
continue;
}
if(ans[i] != 0) {
k += ans[i]-'0';
}
if(shu[i] != 0) {
k += shu[i]-'0';
}
if(k >= 10) {
ans[i] = k-10+'0';
k = 1;//进位
} else {
ans[i] = k+'0';
k = 0;//不进位
}
}
if(k == 1) {//如果超出三个的话加上逗号
if(h-4 >= 0 && (ans[h-4] == ','||ans[h-4]=='.')){
ans[h++] = ',';
}
ans[h++] = k+'0';
}
}
printf("$");
cnt = strlen(ans);
for(int i = cnt-1; i >= 0; i--) {
printf("%c",ans[i]);
}
printf("\n");
}
return 0;
}
很无语,简单题,写了很长时间,各种坑,各种无奈,最近状态又有点不好
相关文章推荐
- zoj3549 快速幂
- ZOJ 1002
- ZOJ-3861 DFS+回溯
- ZOJ3279 —— 8G island
- zoj_1004-Anagrams by stack
- zoj_2278-Fight for Food
- ZOJ 1001 A+B Problem
- zoj 2710 Two Pipelines
- zoj 386 4000 1 Valid Pattern Lock
- ZOJ 3755 Mines 回溯+剪枝
- ZOJ3750 Dot Dot Dot 枚举状态+BFS
- poj 1514&zoj 1185 Metal Cutting(半平面交)
- ZOJ1101-Gamblers 终于AC。。。
- zoj 1148 The Game 一个晚上终于AC!
- zoj 3420 纯bfs
- PAT 01-复杂度2. Maximum Subsequence Sum (25)&&PAT 01-复杂度1. 最大子列和问题(20)
- 狗狗40题~ (Volume A)
- 狗狗40题~ (Volume B)
- 狗狗40题~ (Volume C)
- ZOJ3319 DP 通过入度出度判非法情况