結果

問題 No.3658 Darumaka Number 2
コンテスト
ユーザー kawaura0613
提出日時 2026-08-30 14:24:18
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 5 ms / 2,000 ms
+ 930µs
コード長 1,303 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,166 ms
コンパイル使用メモリ 185,860 KB
実行使用メモリ 9,920 KB
最終ジャッジ日時 2026-10-26 14:23:18
合計ジャッジ時間 5,218 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます
コンパイルメッセージ
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