题目1168: 账单(指针专题)
时间限制: 1 Sec 内存限制: 128 MB
提交: 5694 解决: 3508
每到月末,小明就会对这个月的支出账单进行整理和统计。如今电脑已经普及大学校园,所以小明想让电脑帮忙做这件事情。聪明的你就为小明编一个程序来完成这件事情吧。
输入
多实例测试。首先输入一个整数ncase,表示测试实例的个数。每个测试实例的输入如下:
第一行是整数n (n<100)。然后是n行的账单信息,每一行由事物的名字name和对应的花费c组成,长度不超过200。中间会有一个或多个空格,而每一行的开头和结尾没有空格。 0.0 < c < 1000.0。
输出
每个测试实例对应一行输出,输出总的花费,小数点后保留一位数字。
样例输入 Copy
2
1
Buy books 62.28
3
Apple 2.3
Buy clothes for girl friend 260.5
Go to cinema 30
样例输出 Copy
62.3
292.8
代码
#include <string>
#include <iostream>
using namespace std;
int main() {
int Kase, NKase;
string line;
double num_part;
int index;
int len;
double sum;
string ch;
cin >> NKase;
while(NKase--) {
sum = 0;
cin >> Kase;
getchar();
while (Kase--) {
getline(cin, line);
//cout << line << endl;
auto it = line.end() - 1;
//cout << *it << endl;
for (; (it != line.begin() - 1) && ((isdigit(*it)) || (*it == '.')); it--) {
ch = *it;
//cout << ch << endl;
}
index = line.find(ch);
len = line.size() - index;
num_part = stod(line.substr(index, len));
sum += num_part;
}
printf("%.1lf\n", sum);
}
return 0;
}
总结
有些问题是编译器的限制产生的。。。