3月10日复盘

· · 个人记录

第一题:【深基7.例7】计算阶乘

没什么难度,常规递归即可,代码如下:


#include<bits/stdc++.h>
using namespace std;
int fun(int n)
{
    if(n==1)
    {
        return 1;
    }
    return n*fun(n-1);
}
int main()
{
    int n;
    cin>>n;
    cout<<fun(n);
}

第二题:求 f(x,n)

依旧没有难度,注意具体的规则就能轻松写出,代码如下:

#include<bits/stdc++.h>
using namespace std;
double x;
double fun(double n)
{
    if(n==1)
    {
        return sqrt(1+x);
    }
    return sqrt(n+fun(n-1));
}
int main()
{
    double n;
    cin>>x>>n;
    cout<<fixed<<setprecision(2)<<fun(n);
}

第三题:再求 f(x,n)

。。。懒得说了,只是把根号变成x/了,上代码:

#include<bits/stdc++.h>
using namespace std;
double x;
double fun(double n)
{
    if(n==1)
    {
        return x/(1+x);
    }
    return x/(n+fun(n-1));
}
int main()
{
    double n;
    cin>>x>>n;
    cout<<fixed<<setprecision(2)<<fun(n);
}

第四题:吓得我抱起了抱着抱着抱着我的小鲤鱼的我的我的我

怎么难度又回到第一题了???常规递归,上代码:

#include<bits/stdc++.h>
using namespace std;
void fun1(int n)
{
    if(n==1)
    {
        return;
    }
    cout<<" bao zhe";
    fun1(n-1);
}
void fun2(int n)
{
    if(n==1)
    {
        return;
    }
    cout<<" de wo";
    fun2(n-1);
}
int main()
{
    int n;
    cin>>n;
    cout<<"xia de wo bao qi le";
    fun1(n);
    cout<<" wo de xiao li yu";
    fun2(n);
}

第五题:判断元素是否存在

感觉类似搜索,用反着推更好理解,代码:

#include<bits/stdc++.h>
using namespace std;
int k;
bool check(int x)
{
    if(x==k)
    {
        return true;
    }
    if((x-1)%2==0)
    {
        check((x-1)/2);
    }
    if((x-1)%3==0)
    {
        check((x-1)/3);
    }
    return false;
}
int main()
{
    char ch;
    int x;
    cin>>k>>ch>>x;
    if(check(x))
    {
        cout<<"YES";
    }
    else
    {
        cout<<"NO";
    }
}

第六题:汉诺塔

难度飙升,这题需要寻找规律,把每个步骤分为三部分搜

#include<bits/stdc++.h>
using namespace std;
void fun(int n,char a,char b,char c)
{
    if(n==1) //结束,返回
    {
        cout<<a<<"->1->"<<c<<endl;
        return;
    }
    fun(n-1,a,c,b);//搜
    cout<<a<<"->"<<n<<"->"<<c<<endl;//中间的点直接输出
    fun(n-1,b,a,c);//搜
}
int main()
{
    char a,b,c;
    int n;
    cin>>n>>a>>b>>c;
    fun(n,a,c,b);
    return 0;
}

第七题:波兰表达式

这题其实就是算前缀算式,可以用栈来做,而递归跟栈差不多,所以这题递归比循环更好理解

#include<bits/stdc++.h>
using namespace std;
double fun()
{
    char s[100];
    cin>>s;
    switch(s[0])//每次递归都要重新更新s[]中的内容 
    {
        case '+': return fun()+fun();//加
        case '-': return fun()-fun();//减
        case '*': return fun()*fun();//乘
        case '/': return fun()/fun();//除
        default : return atof(s);//将字符串转化成数字 
        break;
    }
}
int main()
{
    printf("%lf\n",fun());
    return 0;
}

第八题:南蛮图腾

大的三角形是由三个小三角形组成的,所以可以通过大三角形的三个角的坐标来推出小三角形

#include<bits/stdc++.h>
using namespace std;
char ans[4000][4000];//答案数组
void dfs(int x,int y,int n)//n是当前的图形大小
{
    if(n==1)//大小为一的图形直接存
    {
        ans[x][y]='/';
        ans[x][y+1]='\\';
        ans[x+1][y-1]='/';
        ans[x+1][y]='_';
        ans[x+1][y+1]='_';
        ans[x+1][y+2]='\\';
        return;
    }
    dfs(x,y,n-1);//递归搜
    dfs(x+pow(2,n-1),y-pow(2,n-1),n-1);
    dfs(x+pow(2,n-1),y+pow(2,n-1),n-1);
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=pow(2,n);i++)
    {
        for(int j=1;j<=pow(2,n+1);j++)
        {
            ans[i][j]=' ';
        }
    }
    dfs(1,pow(2,n),n);
    for(int i=1;i<=pow(2,n);i++)
    {
        for(int j=1;j<=pow(2,n+1);j++)
        {
            cout<<ans[i][j];
        }
        cout<<endl;
    }
    return 0;
}