题解 | 反序数

反序数

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


#include <cmath>
#include <iostream>
using namespace std;

int func(int x);

int main() {
    int a=1000;
    while (a<=1111) { // 注意 while 处理多个 case
        int b = func(a);
        if(a*9==b){
            cout<<a<<endl;
        }
        a++;
    }

    // int b = func(a);
    // cout<<b;
}

int func(int x){
    int res=0;
    while (x>0) {
        res*=10;
        res += x%10;
        x/=10;
    }
    return res;
}
// 64 位输出请用 printf("%lld")

刷题记录01

知识点:对整数的取反;

注意点:先res*=10,再res += x%10,顺序不能乱; 因为res一开始等于0,res*=10放在第一行,相当于整个遍历过程,会少执行一次res*=10;乘以10的次数刚刚好;

全部评论

相关推荐

09-17 10:53
四川大学 C++
loveTy:你这些技能对大厂没用,而且四川大学因为之前地铁那个事件上了不少民营企业的黑名单。 去试一试国企,他们的黑名单没民营那么狠
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务