HDU 2772 Matchsticks 【数学+规律】


传送门:HDU 2772


Matchsticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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=211
n=377
n=4411
n=5271
n=66111
n=78711
n=8101111
n=9187111
n=102211111
n=112071111
n=1228111111
n=1368711111
n=14881111111
n=151087111111
n=1618811111111
n=1720071111111
n=18208111111111
n=19288711111111
n=206881111111111
n=218887111111111
n=22108811111111111
n=23188871111111111
n=242008111111111111
n=252088711111111111
n=2628881111111111111
n=2768887111111111111
n=28888811111111111111
n=291088871111111111111

最大值的规律就是偶数的最大值就是看可以摆几个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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值