cf-538B Quasi Binary【贪心】

cf-538B Quasi Binary

                    time limit per test2 seconds   memory limit per test256 megabytes

A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 — are quasibinary and numbers 2, 12, 900 are not.

You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.

Input
The first line contains a single integer n (1 ≤ n ≤ 106).

Output
In the first line print a single integer k — the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.

In the second line print k numbers — the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn’t matter. If there are multiple possible representations, you are allowed to print any of them.

input
9
output
9
1 1 1 1 1 1 1 1 1
input
32
output
3
10 11 11

题目思路:贪心。比如说32–>3: 1 – 1 – 1 2: 1 – 1 – 0 所以,答案为11,11,10.

题目链接:cf 538B

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector <int> a;
    int maxsize = 0;
    for(int i = 0; n; i++)
    {
        a.push_back(n % 10);
        if (n % 10 > maxsize)
            maxsize = n % 10;    //求出可以拆分成几个数字
        n /= 10;
    }
    reverse(a.begin(),a.end());
    cout << maxsize << endl;
    for (int i = 0; i < maxsize ;i++)
    {
        bool flag = 0;
        for (int j = 0; j < a.size(); j++)
        {
            if (a[j] > 0)         
            {
                flag = 1;
                a[j]--;
                cout << 1;
            }
            else if (flag)
                cout << 0;
        }
        cout << " ";
    }
    return 0;
}

你可能感兴趣的:(贪心)