Gym - 101102B-------枚舉法



Using at most 7 matchsticks, you can draw any of the 10 digits as in the following picture:
The picture shows how many sticks you need to draw each of the digits.

Zaytoonah has a number that consists of N digits. She wants to move some sticks (zero or more) to maximize the number. Note that she doesn’t want to remove any of the sticks, she will only move them from one place to another within the N digits. She also doesn’t want to add new digits as N is her lucky number.

Can you help Zaytoonah maximize her number?
Input

The first line of input contains a single integer T, the number of test cases.

Each test case contains a single integer N (1 ≤ N ≤ 105), followed by a space, then N digits that represent the number Zaytoonah currently has.
Output

For each test case, print on a single line the maximum number Zaytoonah can get.
Example
Input

3
1 3
3 512
3 079

Output

5
977

997

思路:

在木棍數量確定的前提下,通過移動次數,來使得擺出的數量最大,我們能夠得到十個數字每一個數字所示有的木棍的個數(它們在2-7之間),那麼在確定一根的前提下,剩下的根數需要在 n-1 * 2   ~ n-1*7 之間,每一位都從大到小進行枚舉,即可找到最大的情況

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[15]={6,2,5,5,4,5,6,3,7,6};
char s[100100];
int main ()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %s",&n, s);
        m=strlen(s);
        int sum=0;
        for(int i=0; i<m;i++)
        {
            sum+=a[s[i]-'0'];
        }
        memset(s,0,sizeof(s));
        for(int i=1;i<=n;i++)
        {
            for(int j=9;j>=0;j--)
            {
                if(sum-a[j]>=2*(n-i) && sum-a[j]<=7*(n-i))
                {
                    s[i-1]=j+'0';
                    sum-=a[j];
                    break;
                }
            }
        }
        puts(s);
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章