はまやんはまやんはまやん

hamayanhamayan's blog

Classroom Watch [Codeforces Round #441 A]

http://codeforces.com/contest/875/problem/A

Nが与えられ、X+(Xの各桁の総和)=Nを満たすXを全て答えよ。
1≦N≦10^9

解法

http://codeforces.com/contest/875/submission/31389111
まず、XはNを越えることはない。
そして、Xの最大値が10^9ということは、(Xの各桁の総和)は最大90くらい。
よって、[N-100,N]が答えの候補となりうる。
これを全てチェックして当てはまるやつが答え(下では一応N-1000でやってる)。

int N;
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;

    vector<int> ans;
    rep(i, max(0, N - 1000), N + 1) {
        int sm = i, x = i;
        while (x) {
            sm += x % 10;
            x /= 10;
        }
        if (sm == N) ans.push_back(i);
    }

    int n = ans.size();
    printf("%d\n", n);

    if (n) {
        rep(i, 0, n) {
            if (i) printf(" ");
            printf("%d", ans[i]);
        } printf("\n");
    }
}