Hdu 4493

题目链接

注意四舍五入,保留到小数点后两位(如果存在的话)。

附上代码:

 1 /*************************************************************************
 2     > File Name: 4493.cpp
 3     > Author: Stomach_ache
 4     > Mail: sudaweitong@gmail.com
 5     > Created Time: 2014年05月19日 星期一 22时39分19秒
 6     > Propose: 
 7  ************************************************************************/
 8 
 9 #include <cmath>
10 #include <string>
11 #include <cstdio>
12 #include <fstream>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 
18 int
19 main(void) {
20       int t;
21     scanf("%d", &t);
22     while (t--) {
23           double avg = 0.0;
24         for (int i = 0; i < 12; i++) {
25               double a;
26             scanf("%lf", &a);
27             avg += a;
28         }
29         avg /= 12;
30         char str[30];
31         // 四舍五入
32         sprintf(str, "$%.2f", avg);
33         int len = strlen(str);
34         if (str[len-1] == '0' && str[len-2] == '0') {
35               str[len-3] = '\0';
36         } else if (str[len-1] == '0') {
37               str[len-1] = '\0';
38         }
39         puts(str);
40     }
41 
42     return 0;
43 }

 

posted on 2014-05-19 23:37  Stomach_ache  阅读(419)  评论(0编辑  收藏  举报

导航