B. Quasi Binary

_Yxc發表於2024-03-09

It's a simple problem on codeforces.
we traverse the through the string n, if we encouter a '1', we add a new string to ans and set stop to false.Otherwise, we stop the loop.
Once we find 1, we then append either '1' or '0' to the newly added string in 'ans', based on the next value we traverse.

https://codeforces.com/problemset/problem/538/B

void solve(){
    int n;
    cin >> n;

    string s = std::to_string(n);

    int m = s.size();
    vector<string> ans;
    while (true){
        bool stop = true;
        for (int i = 0; i < m; ++i){
            if (s[i] >= '1' && stop == true){
                ans.push_back("1");
                s[i] --;
                stop = false;
            }
            else if (stop == false){
                if (s[i] >= '1'){
                    ans.back() += "1";
                    s[i] --;
                }
                else{
                    ans.back() += "0";
                }
            }
        }
        if (stop){
            break;
        }
    }

    cout << ans.size() << '\n';
    for (const string& x : ans){
        cout << x << " ";
    }
    cout << '\n';
}

相關文章