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 <iostream>
#include <cstdio>
using namespace std;
int main() {
int t, n;
while(scanf("%d", &n) && n) {
double ans = 0;
t = n;
for(int i = 1; i <= n; i++) {
if(t == 1) {
ans += 1.0*i/t;
} else {
ans += 2.0*i/t;
}
t --;
}
printf("%.2lf\n", ans);
}
return 0;
}