Codeforces Gym

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
题意:略。
方法:贪心,先求出总的火柴数,再从最大数字开始贪,最后一个单独输出。
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+5;
int a[10]={
    6,2,5,5,4,5,6,3,7,6    
};
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        char s[N];
        int sum=0;
        scanf("%d %s",&n,s);
        for(int i=0;i<n;i++)
        {
            sum+=a[s[i]-'0'];
        }
        for(int i=1;i<n;i++)
        {
            for(int j=9;j>=0;j--)
            {
                int temp=sum-a[j];
                if(temp>=2*(n-i)&&temp<=7*(n-i))
                {
                    cout<<j;
                    sum-=a[j];
                    break;
                }
            }
        } 
        for(int i=9;i>=0;i--)
        {
            if(sum==a[i])
            {
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/widsom/p/6739985.html