HDU2772 Matchsticks
来源:互联网 发布:鹅绒比鸭绒暖和吗 知乎 编辑:程序博客网 时间:2024/06/03 20:13
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2772
题目大意: 用火柴拼出0到9的数字:
数字: 1 2 3 4 5 6 7 8 9 0
火柴数: 2 5 5 4 5 6 3 7 6 6
给出n条火柴,问能够拼出的数字中最小和最大分别是多少?
解题思路: 简单的模拟题(由于数字太大,打表不了):
1.拼出的数字最小,就要使用 需要火柴数较多 的数字,使得这串数字总位数最小;
2.拼出的数字最大,则使用 需要火柴数较少 的数字,使得这串数字总位数最大:
看数字7和数字1,他们所需的火柴数最小,并且2和3相差1,可以凑成任意的n (n>=2) ;
把n=22之前的都打印出来就可以很容易的找到规律了:
n=2 1 1
n=3 7 7
n=4 4 11
n=5 2 71
n=6 6 111
n=7 8 711
n=8 10 1111
n=9 18 7111
n=10 22 11111
n=11 20 71111
n=12 28 111111
n=13 68 711111
n=14 88 1111111
n=15 108 7111111
n=16 188 11111111
n=17 200 71111111
n=18 208 111111111
n=19 288 711111111
n=20 688 1111111111
n=21 888 7111111111
n=22 1088 11111111111
n=23 1888 71111111111
n=24 2008 111111111111
n=25 2088 711111111111
n=26 2888 1111111111111
n=27 6888 7111111111111
n=28 8888 11111111111111
n=29 10888 71111111111111
n=30 18888 111111111111111
最小的数:
从15开始周期T=7,每个元素分别是108 188 200 208 288 688 888,每加一个周期多一个8;
最大的数:
从2开始 周期T=2,每个元素分别是1 7,每加一个周期多一个1;
#include <stdio.h>#include <string.h>int main(){ freopen("in.txt","r",stdin); int t,n,bs1,ys1,bs2,ys2; scanf("%d",&t); while(t--) { scanf("%d",&n); if(n <= 1) return 0; if(n == 2) printf("1 1\n"); else if(n == 3) printf("7 7\n"); else if(n == 4) printf("4 11\n"); else if(n==5) printf("2 71\n"); else if(n == 6) printf("6 111\n"); else if(n == 7) printf("8 711\n"); else if(n == 8) printf("10 1111\n"); else if(n == 9) printf("18 7111\n"); else if(n == 10) printf("22 11111\n"); else if(n == 11) printf("20 71111\n"); else if(n == 12) printf("28 111111\n"); else if(n == 13) printf("68 711111\n"); else if(n == 14) printf("88 1111111\n"); else { bs1 = ( n - 15 ) / 7; ys1 = (n - 14) % 7; if(ys1 == 1) printf("108"); else if(ys1 == 2) printf("188"); else if(ys1 == 3) printf("200"); else if(ys1 == 4) printf("208"); else if(ys1 == 5) printf("288"); else if(ys1 == 6) printf("688"); else if(ys1 == 0) printf("888"); if(bs1!=0) while(bs1--) printf("8"); bs2 = (n - 2) / 2; ys2 = n % 2; printf(" "); if(ys2 == 0) printf("1"); else printf("7"); while(bs2 --) printf("1"); printf("\n"); } } return 0;}
- HDU2772 Matchsticks
- HDU2772 Matchsticks 【模拟题】
- Matchsticks
- HDU 2772 Matchsticks
- LeetCode473: Matchsticks to Square
- 473. Matchsticks to Square
- LeetCode: Matchsticks to Square
- LeetCode: Matchsticks to Square
- 473. Matchsticks to Square**
- 473. Matchsticks to Square
- 473. Matchsticks to Square
- 473. Matchsticks to Square
- [LeetCode] Matchsticks to Square
- Leetcode: Matchsticks to Square
- 473. Matchsticks to Square
- Matchsticks to Square
- 473. Matchsticks to Square
- 473. Matchsticks to Square
- D3D中的AGP内存、系统内存、显存的理解
- C++ #include头文件写法的区别
- HDU1671 Phone List
- 【小蒙淘金】4月10日-午夜金评
- IOS学习笔记3
- HDU2772 Matchsticks
- 数组中连续子序列的最大和及子串(js实现)
- UVA 10189
- python: 安装mysql遇到的问题
- struct和typedef struct
- c# HttpWebRequest与HttpWebResponse
- 2013.4.11
- CentOS 设置 pptpd VPN 步骤
- linux 下minicom常用设置