B - The Little Match Girl Gym - 101102B 枚举技巧 火柴棒问题

题目链接:https://cn.vjudge.net/contest/176446#problem/B

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
图中显示了每个数字所需绘制的条数。
Zaytoonah有一个号码,由N位数。她想移动一些棍子(零或更多)来最大化数字。注意,她不想去掉任何一根木棍,她只会把它们从一个地方移到另一个地方。她也不想增加新的数字,因为n是她的幸运数字。
你能帮助Zaytoonah提高她的号码吗?

如果放了这个数字 剩余木棍数 >= 剩余位数×2 && 剩余木棍数 <= 剩余位数×7
因为每一位消耗都是大于等于2 小于等于7的

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <cmath>
#include <queue>
#define mod 1000000007
using namespace std;
int num[12]={6,2,5,5,4,5,6,3,7,6};
int main()
{
   int t;
   cin>>t;
   char a[200000];
   while(t--)
   {
       int n,m;
       cin>>n>>a;
          int sum  =0;
          for(int i=0;i<n;i++)
            sum+=num[a[i]-'0'];

       for(int i=0;i<n;i++)
       {
           for(int j=9;j>=0;j--)
           {
               int u = sum-num[j];
               if(u>=2*(n-i-1)&&u<=7*(n-i-1))
               {
                   a[i]=(j+'0');
                   sum-=num[j];
                   break;
               }
           }
       }
       cout<<a<<endl;
   }
    return 0;
}

另一种方法:完全模拟先吧每一位都填上1,然后从左往右扫,加入sum够就把该位换成9,最后如果木棒多余在从右往左扫,此时要多利用剩下的木棒,假如该位填上了9,就替换成比9用木棒数多的8来尽可能多的消耗木棒,假如填的是1,说明填该位的时候木棒数不够9,那就更不用说填8了,所以就应该替换成使用火柴棒比9少且数字尽可能大的 5或者是4

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <cmath>
#include <queue>

using namespace std;

int main()
{
    int o,n,s,i;
    int a[10]={6,2,5,5,4,5,6,3,7,6};
    scanf("%d",&o);
    s=0;
    char b[111111];
    while(o--)
    {
        scanf("%d%s",&n,b);
        s=0;
        for(i=0;b[i];i++)
        {
            s+=a[b[i]-'0'];
            b[i]='1';
        }
        s-=n*2;
        for(i=0;b[i];i++)
        {
            if(s==0)break;
            if(s>=4)
            {
                s-=4;
                b[i]='9';
            }
            else
            {
                s--;
                b[i]='7';
            }
        }
        if(s>0)
        {
            for(i=n-1;i>=0;i--)
            {
                if(s==0)break;
                if(b[i]=='7')
                {
                    if(s==1){b[i]='4';s=0;break;}
                    if(s==2){b[i]='5';s=0;break;}
                }
                if(b[i]=='9')
                {
                    s--;
                    b[i]='8';
                }
            }
        }

        printf("%s\n",b);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值