1 条题解

  • 0
    @ 2025-4-7 21:28:47

    C :

    #include<stdio.h>
    
    	double myfunction(int n,double x)
    	{
    		if(0==n)
    		return 1;
    		else if(1==n)
    		return x;
    		else return((2*n-1)*x*myfunction(n-1,x)-(n-1)*myfunction(n-2,x))/n;
    	}
    int main()
    	{
    		int n;
    		double x;
    		double result;
    		scanf("%d %lf",&n,&x);
    		result=myfunction(n,x);
    		printf("%.4f\n",result);
    		return 0; 
    		
    	}
    

    C++ :

    #include <stdio.h>
    #include <math.h>
    int main() {
    	float legendre(int n, float x);
    	int n;
    	float x;
    	scanf("%d%f", &n, &x);
    	printf("%.4f\n", legendre(n, x));
    	return 0;
    }
    float legendre(int n, float x) {
    	if (n == 0)
    		return 1;
    	if (n == 1)
    		return x;
    	return ((2 * n - 1) * x * legendre(n - 1, x) - (n - 1) * legendre(n - 2, x)) / n;
    }
    
    

    Pascal :

    
    
    
    var
      n:longint;
      x:double;
    
    function legendre(n:longint; x:double):double;
    begin
      if(n=0) then exit(1);
      if(n=1) then exit(x);
      legendre:=( (2*n-1)*x*legendre(n-1,x)-
        (n-1)*legendre(n-2,x) ) / n;
    end;
    
    begin
      readln(n,x);
      writeln( legendre(n,x):0:4 );
    end.
    
    
    
    
    
    
    
    
    • 1

    信息

    ID
    1334
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者