当前位置 博文首页 > 文章内容

    反序数--清华考研机试

    作者: 栏目:未分类 时间:2020-07-29 16:00:59

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    自己尝试使用的方法:超时了

    #include <cstdio>
    #include <iostream>
    #include<string>
    #include<cstring>
    using namespace std;
    int main()
    {
        for(int n=1000;n<=1111;n++)
        {
            int a,b,c,d,e,f,g,h;
            int m=9*n;
            a=n-(n/10)*10;
            n=n/10;
            b=n-(n/10)*10;
            n=n/10;
            c=n-(n/10)*10;
            n=n/10;
            d=n-(n/10)*10;
            
            e=m-(m/10)*10;
            m=m/10;
            f=m-(m/10)*10;
            m=m/10;
            g=m-(m/10)*10;
            m=m/10;
            h=m-(m/10)*10;
            
            if(a==h&&b==g&&c==f&&d==e)
            cout<<n<<endl;
            
        }
        return 0;
    } 

    看答案,自己又想了一个

    #include <cstdio>
    #include <iostream>
    #include<string>
    #include<cstring>
    using namespace std;
    int re(int a)
    {
        int b=0;
        b+=a/1000;
        a=a-a/1000*1000;
        b+=a/100*10;
        a=a-a/100*100;
        b+=a/10*100;
        a=a-a/10*10;
        b+=a*1000;
        return b;
    }
    int main()
    {
        for(int n=1000;n<=9999;n++)
        {
            if(n*9==re(n))
            cout<<n<<endl;
        }
        return 0;
    } 

    但是也出了一些问题,在re函数中,一开始没给b赋值,导致b一直累加,所以最后没得出结果。

    答案使用的方法更简洁,而且有一定的思维量。

    #include <cstdio>
    #include <iostream>
    #include<string>
    #include<cstring>
    using namespace std;
    int re(int a)
    {
        int b=0;
        while(a!=0)//四次循环
        {
            b*=10;// 0*10 4*10 43*10 432*10 乘四次 
            b+=a%10;
            a=a/10;//1234 123 12 1 0 
         } 
        return b;
    }
    int main()
    {
        for(int n=1000;n<=9999;n++)
        {
            if(n*9==re(n))
            cout<<n<<endl;
        }
        return 0;
    }