传送门:HDU 2772
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.
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.
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.
Sample Input
4
3
6
7
15
Sample Output
7 7
6 111
8 711
108 7111111
题意:
给你n个火柴棍,问你能拼出的最大值和最小值分别是什么。
题解:
一开始想的是贪心,最大值很好想,就是先摆1,能摆几个是几个,当火柴数剩3个的时候摆7就行。
最小值其实思路也很好想,无非就是能摆少的位数,就不摆多的位数,用较多的火柴摆小的数,但是实现不好实现。于是我就想看看能不能找到规律了。
我列到n=29的时候找到的规律:
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 |
最大值的规律就是偶数的最大值就是看可以摆几个1,奇数的最大值就是先摆给7,剩余的都摆1。
最小值的规律:
n<11 最小值无规律
n>=11 每七个一个周期,这个周期的数比上个周期的数末尾+8,如:n=11 20;n=18 208;
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int n,t;
int a[8]={20,28,68,88,108,188,200};
int b[10]={1,7,4,2,6,8,10,18,22};
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
if(n<11)
{
printf("%d ",b[n-2]);
if(n%2==0)
{
for(int i=0;i<n/2;i++)
{
printf("1");
}
printf("\n");
}
else
{
printf("7");
for(int i=0;i<(n-3)/2;i++)
{
printf("1");
}
printf("\n");
}
}
else
{
int temp=(n-11)/7;
int tmp=(n-10)%7;
if(tmp==0) tmp=7;
if(temp==0)
{
printf("%d ",a[tmp-1]);
}
else
{
printf("%d",a[tmp-1]);
for(int i=0;i<temp;i++)
{
printf("8");
}
printf(" ");
}
if(n%2==0)
{
for(int i=0;i<n/2;i++)
{
printf("1");
}
printf("\n");
}
else
{
printf("7");
for(int i=0;i<(n-3)/2;i++)
{
printf("1");
}
printf("\n");
}
}
}
return 0;
}