30pts求调
查看原帖
30pts求调
1314767
RoutineZhou楼主2025/1/25 21:37
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
string s, t;
int a[500], b[500], ans[1000];
int main(){   
    //freopen("P2142_2.in","r",stdin);
    //freopen("P2142_2.out","w",stdout);
    cin >> s >> t;
    if(s == t){ cout << 0;return 0;}
    else if(s.length() < t.length() || (s.length() == t.length()) && s < t){
        string d;
        d = s;
        s = t;
        t = d;
        cout << '-';
    }
    int la = max(s.length(),t.length());
    for(int i = s.length()-1, j = 1; i >= 0; i--, j++) a[j] = s[i] - '0';
    for(int i = t.length()-1, j = 1; i >= 0; i--, j++) b[j] = t[i] - '0';
    for(int i = 1; i <= la; i++){
        ans[i] += a[i] - b[i];
        if(ans[i] < 0){
            ans[i] += 10;
            ans[i+1] -= 1;
        } 
    }
    int len2; 
    for(len2 = la; len2 >= 1; len2--){
        if(ans[len2] != 0){
            break;
        }
    }
    for(int i = len2; i >= 1; i--){
        cout << ans[i];
    }
    return 0;
}
2025/1/25 21:37
加载中...