Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)

传送门

Description

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.

Sample Input

3
1 3
3 512
3 079

Sample Output

5
977
997

思路

题意:

给出0-9十个数字,每个数字用固定的火柴棍搭成,给出由N个字符的构成数字串,只能移动火柴并且移动后数字的个数也是N个,问怎么移动火柴使数字值最大

题解:

每个数字的火柴根数固定,并且数字“1”所需要的火柴是十个数字中最少的,因此假定一开始全部都是由“1”组成N个数字,然后用剩下的火柴棍从前往后扫尽量使之最大。

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;

int main()
{
	int T;
	int num[] = {6,2,5,5,4,5,6,3,7,6};
	scanf("%d",&T);
	while (T--)
	{
		int N,sum = 0;
		char a[maxn];
		scanf("%d %s",&N,a);
		for (int i = 0;i < N;i++)	sum += num[a[i]-'0'];
		sum -= 2*N;
		for (int i = 0;i < N;i++)
		{
			if (sum >= 4)
			{
				sum -= 4;
				a[i] = '9';
			}
			else if (sum > 0 && sum < 4)
			{
				for (int j = 9;j >= 0;j--)
				{
					if ((num[j] <= sum + 2 && i != N - 1) || num[j] == sum + 2)
					{
						sum -= num[j] - 2;
						a[i] = j + '0';
						break;
					}
				}
			}
			else
			{
				a[i] = '1';
			}
		}
		for (int i = N - 1;i >= 0 && sum > 0;i--)
		{
			a[i] = '8';
			sum--;
		}
		printf("%s\n",a);
	}
	return 0;
}

 

  

 

转载于:https://www.cnblogs.com/ZhaoxiCheung/p/6028894.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值