Problem 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 elements on the diagonal of the matrix are always 1/1, the diagonal The denominators of the fractions on both sides increase one by one.
Request the sum of this matrix.
Input is
given an integer N (N<50000) for each row, which means that the matrix is N*N. When N is 0, the input ends.
Output
outputs the answer with 2 decimal places.
#include <stdio.h>
int a[50001];
/*没什么好说的,
就是找规律;
列如:5*5的数组
数字: 1 2 3 4 5
出现次数: 5 8 6 4 2
所以就很好计算了,每次乘以出现次数累加就行;`在这里插入代码片`
*/
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
double sum=0;
int i,k=n,m,num=2;//用数组存放出现次数;
m=n*n;
for(i=n;i>2;i--)
{
a[i]=num;
k+=num;
num+=2;
}
a[1]=n;
a[2]=m-k;
for(i=1;i<=n;i++)//累加
sum+=a[i]*(1.0/i);
printf("%.2lf\n",sum);
}
return 0;
}