题解 | #反序相等#

反序相等

https://www.nowcoder.com/practice/092e7068881549bd8985c379208ef3c2

众所周知C++有许多好用又方便的库函数。

to_string()可以把数字转成对应的字符串;

reverse()函数可以实现字符串的快速翻转;

stringstream 可以方便操作字符串并将其转成数字;

然后……然后就没了(i的九倍也是四位数,i不能超过1111,可以缩短一点循环)

#include <iostream>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    for ( int i = 1000; i <= 1111; i ++ ){
        string s = to_string(i);
        reverse(s.begin(),s.end());
        stringstream geek(s);
        int x;
        geek >> x;
        if ( i*9 == x ) cout << i << endl;
    }
    return 0;
}

全部评论

相关推荐

06-07 12:20
新余学院 Java
点赞 评论 收藏
分享
码农索隆:有点耳熟,你们是我教过最差的一届
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

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