zoukankan      html  css  js  c++  java
  • 反序数--清华考研机试

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

    #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;
    } 
  • 相关阅读:
    C#时间差
    centos8安装ffmpeg
    CentOS8同步时间
    安装Supervisor
    ajax 传递 token
    .net core 3.1 中 的跨域设置
    jaeger 本地编译
    Kubernates 环境搭建
    linux : find
    Linux: 文件分割和合并
  • 原文地址:https://www.cnblogs.com/h694879357/p/13397660.html
Copyright © 2011-2022 走看看