Codeforces Gym - 101102B - The Little Match Girl

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值