1 solutions

  • 3
    @ 2024-1-20 18:06:08

    坑人的递归题!!!

    题目所描述的代码:

    #include<iostream>
    #include<cmath>
    using namespace std;
    double f(double x,double n)
    {
    	if(n<=1)
    	{
    		return sqrt(1+x);
    	}
    	else
    	{
    		return sqrt(n+f(x,n-1));
    	}
    }
    int main()
    {
    	double a,b;
    	cin>>a>>b;
    	printf("%.2lf",f(a,b));
    	return 0;
    }
    

    实际上的代码(我查网上了):

    根本就不是平方根!!!

    是除法!!!

    我就说怎么样例对不上!!!

    #include<iostream>
    #include<cmath>
    using namespace std;
    double f(double x,double n)//递归函数
    {
    	if(n<=1)//递归边界
    	{
    		return x/(1+x);//结束递归
    	}
    	else
    	{
    		return x/(n+f(x,n-1));//递归
    	}
    }
    int main()
    {
    	double a,b;
    	cin>>a>>b;//输入
    	printf("%.2lf",f(a,b));//保留两位(题目也没说)!!!
    	return 0;//完结散花
    }
    
    • 1

    Information

    ID
    653
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    33
    Accepted
    16
    Uploaded By