/* 子序列的和(subsequence) 输入两个整数n<m<10^6,输出1/(n^2) + 1/((n+1)^2) + 1/((n+2)^2) 1/((n+3)^2) + ... + 1/((m)^2) 保留5位小数。输入包含多组数据,结束标记为n=m=0。提示:本题目有陷阱 样例输入: 2 4 65536 655360 样例输出: Case 1:0.42361 Case 2:0.00001 */ #include <iostream> #include <math.h> using namespace std; int main(){ int n, m, cases = 0; double result = 0.0; while(scanf("%d %d", &n, &m) == 2){ if((n == m) && ( m == 0)){ break; } cases++; result = 0.0; for(int i = n; i< m; i++){ result += 1. / (long long)(pow(i, 2));//【key 1】10^6,范围;【key 2】1. 运用浮点数运算,否则会有精度损失 } printf("Case %d: %.5f ", cases, result); } return 0; } /* [input] 2 4 65536 655360 0 0 [output] Case 1:0.42361 Case 2:0.00001 */
【参考文献】
刘汝佳.《算法竞赛入门经典》