题意:给定12个数,求平均数。
析:这个题就是精度控制问题,如果控制精度,最好的办法就是用整型了。
代码如下:
#include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> using namespace std ; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f3f; const double eps = 1e-8; const int maxn = 2e5 + 5; const int dr[] = {0, 0, -1, 1}; const int dc[] = {-1, 1, 0, 0}; int n, m; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int a[maxn]; int main(){ int T; cin >> T; while(T--){ double sum = 0; double x; for(int i = 0; i < 12; ++i){ cin >> x; sum += x; } int ans = (int)round(sum * 100.0/12.0); printf("$"); printf("%d", ans/100); int y = ans % 100; if(!y); else{ printf(".%d", y /10); y = y % 10; if(!y) ; else printf("%d", y %10); } printf("\n"); } return 0; }