ZZULIOJ:1168: 账单(指针专题)

题目描述

每到月末,小明就会对这个月的支出账单进行整理和统计。如今电脑已经普及大学校园,所以小明想让电脑帮忙做这件事情。聪明的你就为小明编一个程序来完成这件事情吧。

输入

多实例测试。首先输入一个整数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 <iostream>
#include <cstring>
#define gets(S) fgets(S,sizeof(S),stdin)
using namespace std;
const int N = 10000 + 10;
char a[N];
int main()
{
    int t;
    cin >> t;
    while(t -- )
    {
         
        int n;
        cin >> n;
        double ans = 0;
        getchar();
        while(n -- )
        {
            gets(a);
            char *p = strrchr(a,' ');
            double price;
            sscanf(p,"%lf",&price);
            ans += price;
        }
        printf("%.1lf\n",ans);
    }
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/couchpotatoshy/article/details/126111083
今日推荐