1168: 账单(指针专题)

xiaoxiao2021-02-28  90

Description

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

Input

多实例测试。首先输入一个整数ncase,表示测试实例的个数。每个测试实例的输入如下:

第一行是整数n (n<100)。然后是n行的账单信息,每一行由事物的名字name和对应的花费c组成,长度不超过200。中间会有一个或多个空格,而每一行的开头和结尾没有空格。 0.0 < c < 1000.0

Output

每个测试实例对应一行输出,输出总的花费,小数点后保留一位数字。

Sample Input

21Buy books 62.283Apple 2.3Buy clothes for girl friend 260.5Go to cinema 30

Sample Output

62.3292.8 #include<stdio.h> #include<stdlib.h> #include<string.h> #define N 105 double TotalCost(int n ); int main () { double sum=0.0; int n,t; scanf("%d",&n); while ( n-- ) { scanf("%d",&t); getchar();//消掉回车 sum = TotalCost(t); printf("%.1f\n",sum); } return 0; } double TotalCost(int n ) { char *p; double sum=0.0; double f; p = (char *)malloc(sizeof(char)*N); while ( n-- ) { gets(p); p = strrchr(p,' '); //从后向前查找空格 sscanf(p,"%lf",&f); //从字符串p中读取一个double类型的数据存入f sum+=f; } return sum; }

HINT

转载请注明原文地址: https://www.6miu.com/read-52128.html

最新回复(0)