http://acm.hdu.edu.cn/showproblem.php?pid=2156
Problem Description我们定义如下矩阵:
1/1 1/2 1/3
1/2 1/1 1/2
1/3 1/2 1/1
矩阵对角线上的元素始终是1/1,对角线两边分数的分母逐个递增。
请求出这个矩阵的总和。 Input每行给定整数N (N<50000),表示矩阵为 N*N.当N为0时,输入结束。 Output输出答案,保留2位小数。 Sample Input12340 Sample Output1.003.005.678.83 代码:
#include <bits/stdc++.h>
using namespace std;double a[55555];int main() {
a[0] = 0, a[1] = 1, a[2] = 3;
for(int i = 3; i <= 50001; i ++) {
a[i] = 2 * a[i - 1] - a[i - 2] + 2.0 / i;
} int N;
while(~scanf("%d", &N)) {
if(N == 0) break;
printf("%.2lf\n", a[N]);
}
return 0;
}