题目挺简单,主要是思想不难。但很多小点要注意。
题目:子序列的和(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
0 0
样例输出:
Case 1: 0.42361
Case 2: 0.00001
1 #include <stdio.h> 2 #include <math.h> 3 4 int main () { 5 int n, m; 6 int p = 1; 7 while (scanf ("%d%d", &n, &m) != EOF) { 8 if (n == 0 && m == 0) 9 break; 10 double sum = 0; 11 for (int i = n; i <= m; i++) { 12 sum += 1.0 / (long long)(pow(i, 2)); 13 } 14 printf ("Case %d: %.5f\n", p, sum); 15 p++; 16 } 17 return 0; 18 }
1、 多组数据
while(cin >> m >> n >> t>>s){}
while (~scanf ("%d%d%d%d", &m, &n, &t, &s)) {}
while (scanf ("%d", &a) != EOF) {}
while (scanf ("%d", &a) == 1) {}
2、 continue与break
continue 直接进行下一次循环
break直接跳出循环
3、 小心乘法溢出
4、 平方开根绝对值(#include <math.h>)
几次方 pow(,)
int a = pow(4,2);4^2
int a = pow(4,0.5);4^0.5
开方
int b = sqrt(4);
整数绝对值
int a = abs(b-c);
浮点数绝对值
double a = fabs(b-c);