题解 | #23年OPPO-a的翻转#(string)

23年OPPO-a的翻转

https://www.nowcoder.com/practice/dcce2d0cc8f740c29e0885df96c9d625?tpId=376&ru=%2Fexam%2Foj&qru=%2Fta%2F15-days-help%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E9%259D%25A2%25E8%25AF%2595%26topicId%3D376&difficulty=&judgeStatus=&tags=&title=&gioEnter=menu

既然头和尾要相加,那么如果使用int来存数字并做到翻转相加相对麻烦

那么类似高精度的计算一般,使用char数组来存每一个位置的数字,来简化。

#include "bits/stdc++.h"

using namespace std;
#define int long long
#define endl "\n"
void slu() {
    string a;
    cin >> a;
    int res = 0;
    for (int i = 0; i < a.size(); i++) {
        res *= 10;
        res += (a[i] - '0') + (a[a.size() - i - 1] - '0');
    }
    cout << res;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T;
//    cin >> T;
    T = 1;
    while (T--)slu();

}

全部评论

相关推荐

11-03 18:50
门头沟学院 Java
迷茫的大四🐶:问就是马上到,一周五天,6个月以上,全国可飞
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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