1 条题解

  • 0
    @ 2025-2-14 20:44:41

    C :

    #include<stdio.h>
    #include<math.h>
    int main()
    {
    	int a,s,f,n;
    	double i,j,t,q,r;
    	n=1;
    	f=1;
    	q=0.0;
    	t=1;
    	scanf("%lf",&i);
    	while(t>i)
    	{
    		t=(double)1/n;
    		q=q+t*f;
    		f=-f;
    		n++;
    		r=1/n;
    		if(r<=i)
    		{
    			t=fabs(t);}
    	}
    	printf("%.4lf",q);
    
    	return 0;
    }
    

    C++ :

    #include<stdio.h>
    
    int main()
    {
    	
    	double e, a, b, c, d;
    	scanf("%lf", &e);
    	for(a = 1, c = 0, d = 1.0 ; ( 1 / a ) >= e  ; a++ )
    	{
    		b = d * (1 / a);
    		c = c + b;
    		d = -d;
    	}
    	printf("%.4lf", c);
    	return 0;
    }
    

    Pascal :

    program nghj;
    var
      n,f:integer;
      s,e:real;
      begin
        readln(e);
        n:=1;
        s:=0;
        f:=-1;
        repeat
         f:=f*(-1);
         s:=s+(1/n*f);
         n:=n+1;
    
        until abs(1/(n-1))<=e;
        writeln(s:0:4);
    end.
    
    
    
    
    
    
    • 1

    信息

    ID
    348
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者