Matchsticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 804 Accepted Submission(s): 282
Problem Description
Matchsticks are ideal tools to represent numbers. A common way to represent the ten decimal digits with matchsticks is the following:
This is identical to how numbers are displayed on an ordinary alarm clock. With a given number of matchsticks you can generate a wide range of numbers. We are wondering what the smallest and largest numbers are that can be created by using all your matchsticks.
![](https://i-blog.csdnimg.cn/blog_migrate/2f87a203881ebc621b41854e169f3565.jpeg)
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
* One line with an integer n (2 ≤ n ≤ 100): the number of matchsticks you have.
* One line with an integer n (2 ≤ n ≤ 100): the number of matchsticks you have.
Output
Per testcase:
* One line with the smallest and largest numbers you can create, separated by a single space. Both numbers should be positive and contain no leading zeroes.
* One line with the smallest and largest numbers you can create, separated by a single space. Both numbers should be positive and contain no leading zeroes.
Sample Input
4 3 6 7 15
Sample Output
7 7 6 111 8 711 108 7111111
题意:n个火柴棒所能摆出的最小数和最大数
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=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 [ 15 -> 21 ] 开始 周期 T = 7 每加一个周期多一个 8
最大数 从 n [ 2 -> 3 ] 开始 周期 T = 2 每加一个周期多一个 1
#include<stdio.h>
int ans[22][2]={{0},{0},{1,1},{7,7},{4,11},{2},
{6},{8},{10},{18},{22},{20},
{28},{68},{88},{108},{188},
{200},{208},{288},{688},{888}};
int main()
{
int t;
int i,j;
int num;
int n8,n1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&num);
if(num<=21)
{
printf("%d ",ans[num][0]);
n1=(num-2)/2;
num%2?printf("7"):printf("1");
while(n1--)
{
printf("1");
}
}
else
{
printf("%d",ans[(num-15)%7+15][0]);
n8=(num-15)/7;
while(n8--)
{
printf("8");
}
n1=(num-2)/2;
num%2?printf(" 7"):printf(" 1");
while(n1--)
{
printf("1");
}
}
printf("\n");
}
return 0;
}