The Little Match Girl Gym

来源:互联网 发布:c 输出语言 编辑:程序博客网 时间:2024/05/16 06:09
B. The Little Match Girl
time limit per test
1.0 s
memory limit per test
256 MB
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 asN 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, thenN 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
31 33 5123 079
Output
5977997



1和2 两块代码思路相同,其实你列举出对应关系后会发现。用火柴棍摆出的每一种数字所用的火柴棍数都在2~7之间。

数               消耗

9                   6

8                  7

7                  3

5                  5

4                  4

1                  2

按这个顺序进行贪心,枚举就可以了。

需要注意的是你贪心的条件是    如果放了这个数字  剩余木棍数 >= 剩余位数×2   && 剩余木棍数 <= 剩余位数×7

因为每一位消耗都是大于等于2 小于等于7的。


1.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define inf 0x3f3f3f3fusing namespace std;int p[10]={9,8,7,5,4,1};int v[10]={6,7,3,5,4,2};int a[20]={6,2,5,5,4,5,6,3,7,6};int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n;        char s[112345];        scanf("%d%s",&n,s);        int sum=0,len = strlen(s);        for(int i=0;i<len;i++)        {            sum += a[s[i]-'0'];        }        for(int i=0;i<len;i++)        {            int j=0;            while(sum-v[j]<(n-i-1)*2||sum-v[j]>(n-i-1)*7) j++;            sum -= v[j];            printf("%d",p[j]);        }        printf("\n");    }    return 0;}


2.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define inf 0x3f3f3f3fusing namespace std;int p[10]={9,8,7,5,4,1};int v[10]={6,7,3,5,4,2};int a[20]={6,2,5,5,4,5,6,3,7,6};int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n;        char s[112345];        scanf("%d%s",&n,s);        int sum=0,len = strlen(s);        for(int i=0;i<len;i++)        {            sum += a[s[i]-'0'];        }        for(int i=0;i<len;i++)        {            for(int j=0;j<6;j++)            {                if(sum-v[j]>=2*(n-1-i) && sum-v[j]<=7*(n-1-i))                {                    sum-=v[j];                    printf("%d",p[j]);                    break;                }            }        }        printf("\n");    }    return 0;}