题解 | #反序数#

反序数

https://www.nowcoder.com/practice/e0d06e79efa44785be5b2ec6e66ba898

循环不需要写到9999,因为i*9要小于等于9999,所以循环到1111即可

暴力解:

#include <iostream>
using namespace std;

int main() {
    int a, b, c, d;
    int temp;
    int aa,bb,cc,dd;
    for (int i = 1000; i < 1111; i++){
        temp = i;
        d = temp%10;
        temp /= 10;
        c = temp%10;
        temp /= 10;
        b = temp%10;
        temp /= 10;
        a = temp%10;

        temp = i*9;

        dd = temp%10;
        temp /= 10;
        cc = temp%10;
        temp /= 10;
        bb = temp%10;
        temp /= 10;
        aa = temp%10;

      //  cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
       // cout<<aa<<" "<<bb<<" "<<cc<<" "<<dd<<endl;
        if (d == aa && c == bb && b == cc && a == dd)
            cout << i << endl;
    }
        
    
}
// 64 位输出请用 printf("%lld")

暴力解 简洁代码

#include <iostream>
using namespace std;

int main() {
    int a, b, c, d;
    int temp;
    int aa,bb,cc,dd;
    for (int i = 1000; i < 1111; i++){
        if ((i%10) == (i*9/1000)%10 && (i/10)%10 == (i*9/100)%10 && 
        (i/100)%10 == (i*9/10)%10 && (i/1000)%10 == (i*9)%10)
            cout << i << endl;
    }
     
}

全部评论

相关推荐

ztqiuzhi:直接回,现在已经到你们公司楼下了哦
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务