【題解】ZeroJudge c423: pB 還原密碼

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c423

#include <iostream>
#include <set>
#include <string>
#include <algorithm>
using namespace std;

int n, r;
string s;
set <string> ans;

int f(string str){
    int ret = 0;
    for (int i = 0; i < str.length(); i++){
        ret += (int)(str[i]-'0');
    }
    if (ret < 10) return ret;
    return f(to_string(ret));
}

int main() {
    cin >> n >> r;
    cin >> s;
    for (int i = 0; i < 10; i++){
        if (f(s+(char)('0'+i)) == r){
            for (int j = 0; j < n; j++){
                ans.insert(s.substr(0, j)+char('0'+i)+s.substr(j, n-j-1));
            }
        }
    }
    ans.erase(ans.begin());
    auto it = ans.end();
    it--;
    ans.erase(it);
    for (string i:ans){
        cout << i << "\n";
    }
}
分享本文 Share with friends