HDU - 2156 分数矩阵

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 <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    int n;
    double sum;
    while (~scanf("%d", &n) && n!=0){
        sum = 0;
        for (int i =2 ; i <= n; i++)
            sum += (n-i+1)*2.0/i;
        printf("%.2f\n", sum+n);
    }
    return 0;
}
发布了339 篇原创文章 · 获赞 351 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/Aibiabcheng/article/details/105353315