跳到正文

题解

题解:UVA12172 Matchsticks

tian0828约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
获赞
1
题目
UVA12172

版本

  1. v1当前

挺难:

C++
#include<bits/stdc++.h>
using namespace std;

const int cost[]={6,2,5,5,4,5,6,3,7,6};

int n;

bool inseg(int bef,int ob,int aft)
{return bef<=ob && ob<=aft;}

void Max()
{
    cout<<7*(n%2)+!(n%2);
    for(int i=1;i<n/2;i++) cout<<1;
    cout<<endl;
}

int main()
{
    //freopen("matchsticks.in" ,"r",stdin );
    //freopen("matchsticks.out","w",stdout);
  	int T;
  	scanf("%d",&T);
  	while(T--)
    {
        scanf("%d",&n);
        int temp=ceil(n/7.0),N=n;
  		for(int i=1;i<=temp;i++)//8 的数量
    		for(int j=0;j<10;j++)//枚举取的数字
      			if(i+j>1 && inseg(2*(temp-i),n-cost[j],7*(temp-i)))
                    {cout<<j,n-=cost[j];break;}
        n=N;
  		cout<<' ';
  		Max();
    }
  	return 0;
}

(⁎⁍̴̛ᴗ⁍̴̛⁎)

原文