【題解】AtCoder 155E – Payment

【題目敘述】https://atcoder.jp/contests/abc155/tasks/abc155_e

#include <iostream>
using namespace std;

int n, ans[105], hold;
string s;

void add(int hold){
    for (int i = 0; i < 105; i++){
        ans[i] += hold;
        hold = ans[i] / 10;
        ans[i] %= 10;
        if (!hold) break;
    }
}

int main() {
    cin >> s;
    for (int i = s.length()-1; i >= 0; i--){
        n = s[i]-'0';
        n += hold;
        hold = n / 10;
        n %= 10;
        if (n < 5) add(n);
        else if (n > 5){
            add(10-n);
            hold += 1;
        }
        else{
            if (i != 0 && s[i-1]-'0' >= 5){
                add(10-n);
                hold += 1;
            }
            else add(n);
        }
    }
    add(hold);
    bool flag = false;
    for (int i = 104; i >= 0; i--){
        if (ans[i] != 0) flag = true;
        if (flag) cout << ans[i];
    }
    cout << "\n";
}

分享本文 Share with friends