HDU 2156 分数矩阵

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 Input
1
2
3
4
0
 
Sample Output
1.00
3.00
5.67
8.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;
}

  

猜你喜欢

转载自www.cnblogs.com/zlrrrr/p/9417331.html