题解C++ | #数字颠倒#

数字颠倒

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

关键知识点:

可以使用tostring将int类型数据转为字符串,配合使用reverse方法做翻转输出

#include <iostream>
using namespace std;

int main() {
    long long n;
    cin >> n;

    if (n == 0)
        cout << n;

    while (n > 0) {
        int temp = 0;
        temp = n % 10;
        n = n / 10;
        cout << temp;
    }
}
// 64 位输出请用 printf("%lld")

#include<bits/stdc++.h>
using namespace std;
int main () {
    int nums;
    cin >> nums;
    string str = to_string(nums);
    reverse(str.begin(),str.end());
    cout << str;
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务