結果

問題 No.3658 Darumaka Number 2
コンテスト
ユーザー kawaura0613
提出日時 2026-08-30 14:24:18
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 5 ms / 2,000 ms
+ 68µs
コード長 1,303 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,238 ms
コンパイル使用メモリ 184,132 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2026-08-30 14:24:42
合計ジャッジ時間 3,544 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'bool solve(int, std::string&, std::stack<char>&)':
main.cpp:50:1: warning: control reaches end of non-void function [-Wreturn-type]
   50 | }
      | ^

ソースコード

diff #
raw source code

#include <iostream>
#include <stack>

using namespace std;

bool solve(int i, string &n, stack<char> &ans) {
    if (i == n.size()) {
        return true;
    }

    if (n[i] < '4') {
        // cout << 1 << endl;
        return false;
    }
    if (n[i] > '5') {
        // cout << 2 << endl;
        while (i < n.size()) {
            ans.push('5');
            i++;
        }
        return true;
    }

    if (n[i] == '5') {
        // cout << 3 << endl;
        if (solve(i + 1, n, ans)) {
            ans.push('5');
            return true;
        }
        else {
            i++;
            while (i < n.size()) {
                ans.push('5');
                i++;
            }
            ans.push('4');
            return true;
        }
    }
    if (n[i] == '4') {
        // cout << 4 << endl;
        if (solve(i + 1, n, ans)) {
            ans.push('4');
            return true;
        }
        else {
            return false;
        }
    }
}

int main() {
    string n;
    cin >> n;

    stack<char> ans;
    if (!solve(0, n, ans)) {
        for (int i = 1; i < n.size(); i++) {
            cout << 5;
        }
        cout << endl;
    }
    else {
        while (!ans.empty()) {
            cout << ans.top();
            ans.pop();
        }
        cout << endl;
    }
}
0