doublef(int a, int b) { double sum = 0; for (;a <= b;++a) sum += 1.0 / a / a; return sum; }
intmain() { int n, m,times=0; cout << setprecision(5)<<fixed; do { do { cin >> n >> m; if (n == 0 && m == 0) return0; } while (n >= m || n <= 0); cout << "Case " << ++times << ": " << f(n, m) << endl; } while (1); return0; }
题目:算法竞赛入门经典 2-5 分数化小数 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream> #include<iomanip> usingnamespacestd; intmain() { int a, b, c,CASE=0; while (1) { do { cin >> a >> b >> c; if (a == 0 && b == 0 && c == 0) return0; } while (a > 1e6 || b > 1e6 || c > 100); cout << "Case " << ++CASE << ": " << fixed << setprecision(c) << (double)a / (double)b << endl;; } return0; }