模拟题 找出不能拼凑的最小数

A. An interesting game
Zhouzhou and Dazhongfeng are playing an interesting game. They have
some cards with numbers on them, and all the numbers are between zero and
nine. They can use these cards to make up different numbers. Now they want
to know the smallest number they cannot make up. Can you tell them?

The first line contains an integer T(T 100). The next T lines, each line
contains a string S(
jSj ≤ 100000), the string represents all the cards they have,
each character represents an card.

Output the smallest number they cannot makeup.

3
01234567
2301
1111222

8 4 0


#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int aa[11];
int main()
{
    int num,j;
    string s;
    cin>>num;
    while(num--)
    {
        memset(aa,0,sizeof(aa));
        cin>>s;
        for(int i=0;i<s.length();i++)
        {
            aa[s[i]-'0']++;
        }
        int min=9999999,flag=0;
        for(int i=0;i<10;i++)
        {
            if(aa[i]==0)
            {
                printf("%d\n",i);
                flag=1;
                break;
            }
            if(min>aa[i])
            {
                min=aa[i];
                j=i;
            }
        }
        if(!flag)
        {
            if(j!=0){
           for(int i=0;i<min+1;i++)
           {
               cout<<j;
           }
           cout<<endl;
            }
            else
            {
                cout<<1;
                for(int i=0;i<min+1;i++)
                {
                    cout<<0;
                }
                cout<<endl;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lirui7610/article/details/80185172