Description
We define the following matrix:
1/1 1/2 1/3
1/2 1/1 1/2
1/3 1/2 1/1 The
diagonal elements of the matrix are always 1/1, and the diagonal scores on both sides 'S denominator increases one by one.
Request the sum of this matrix.
Input
Each row is given an integer N (N <50000), indicating that the matrix is N * N. When N is 0, the input ends.
Output
Output the answer, keeping 2 decimal places.
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;
}