Math problems -> Four arithmetic operations of fractions

Question A: Score Matrix

Insert picture description here
Insert picture description here
Idea 1:
This idea is correct, but the data in the title is too large and overtime.
Insert picture description here
Time complexity O(n 2 )

#include<cstdio>
#include<cmath>
double a[50005]={
    
    0};
int main(void)
{
    
    
	int N;
	int i,j;
	a[1]=1;
	a[2]=3;
	while(1)
	{
    
    
		scanf("%d",&N);
		for(i=3;i<=N;i++)
		{
    
    
			double sum=0;
			for(j=1;j<=i;j++)
			{
    
    
				sum+=1.0/j;
			}
			sum=sum*4-2-2.0/(j-1);
			a[i]=sum+a[i-2];
		}
		if(N==0)
			break; 
		printf("%.2lf\n",a[N]);
	}
	return 0;
}*/

Idea 2:
Insert picture description here
Time complexity O(n)

#include<cstdio>
int main(void)
{
    
    
	int N;
	while(1)
	{
    
    
		scanf("%d",&N); 
		if(N==0)
			break;
		double sum=0;
		for(int i=1;i<=N;i++)
		{
    
    
			sum=sum+1.0/i*(N+1-i);
		} 
		printf("%.2lf\n",sum*2-N);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_46527915/article/details/115066928