UVa 10137 The Trip

 /* 本题难点: 如何对实数的小数位进行截取。方法如下: char temp[1000]; double m; sprintf(temp,"%.klf",m); sscanf(temp,"%lf",&m); 使用该方法可以截取实数m小数点后的k位。 */ /* coder: ACboy date: 2010-2-19 result: AC description: UVa 10137 The trip */ #include <stdio.h> int main() { #ifndef ONLINE_JUDGE freopen("10137.txt", "r", stdin); #endif int n; double data[1010]; double average; char temp[1010]; while (scanf("%d", &n) != EOF) { if (n == 0) break; double ans1, ans2; average = 0; for (int i = 0; i < n; i++) { scanf("%lf", &data[i]); average += data[i]; } average /= n; sprintf(temp, "%.2lf", average); sscanf(temp, "%lf", &average); ans1 = ans2 = 0; for (int j = 0; j < n; j++) { if (data[j] > average) ans1 += (data[j] - average); else ans2 += (average - data[j]); } if (ans1 > ans2) printf("$%.2lf/n", ans2); else printf("$%.2lf/n", ans1); } return 0; }

你可能感兴趣的:(Date,2010)