The Little Match Girl Gym
来源:互联网 发布:c 输出语言 编辑:程序博客网 时间:2024/05/16 06:09
Using at most 7 matchsticks, you can draw any of the 10 digits as in the following picture:
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?
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.
For each test case, print on a single line the maximum number Zaytoonah can get.
31 33 5123 079
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;}
- The Little Match Girl Gym
- GYM 101102 B.The Little Match Girl (贪心)
- Gym - 101102B B. The Little Match Girl 贪心、数论、分步
- CF the little match girl (贪心!!!)
- ACM刷题之codeforces————The Little Match Girl
- The Little Girl who Picks Mushrooms
- HDUOJ4422 The Little Girl who Picks Mushrooms
- The Little Girl who Picks Mushrooms
- zoj3657-The Little Girl who Picks Mushrooms
- zoj The Little Girl who Picks Mushrooms
- The Little Girl who Picks Mushrooms HDU
- The Little Girl who Picks Mushrooms(HDU
- The Little Girl who Picks Mushrooms
- The Little Girl who Picks Mushrooms HDU
- ZOJ Problem Set - 3657The Little Girl who Picks Mushrooms
- hdu 4422 The Little Girl who Picks Mushrooms
- hdu 4422The Little Girl who Picks Mushrooms
- ZOJ 3657 The Little Girl who Picks Mushrooms
- JavaScript学习之正则表达式
- 2017多校八 1008题 hdu 6140 Hybrid Crystals 推理
- Linux运维的前景与现状简析
- 关于嵌入式里的volatile
- 在pycharm中配置Anaconda以及pip源配置
- The Little Match Girl Gym
- ajax的增删改功能
- 简单的shell脚本
- 数字开头的字段名问题(随笔)
- 网络编程(五)--仿QQ聊天程序
- hdu 6143 容斥原理+多重集合的排列数 2017 Multi-University Training Contest
- 抽象层次
- c++ 参考网站
- iSCSI