给你些火柴棍,找出能摆的最大大值和最小值

这篇博客通过C++代码展示了如何使用动态规划和搜索技巧找到用特定长度火柴棍可以摆出的最大和最小数字。作者首先定义了火柴棍对应的数字关系,然后分别编写了创建最小值和最大值的函数,并在主函数中进行了测试。
#include <iostream>//本题据说是个动态规划,本人硬着头皮去找规律,所以代码只能说是投机,或者说是暴力去解决问题的。
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

#define PF(x) (scanf("%d",&x))
#define PT(x,y) (scanf("%d%d",&x,&y))
#define PR(x) (printf("%d\n",x))
#define PRT(x,y)(printf("%d %d\n",x,y))
#define M 1000
int ar[10] = {6,2,5,5,4,5,6,3,7,6};
string mins;
bool flag = false;
int L;
string creatmin(int hei)//经过搜索测试发现最多只需要调整三位就可以完成要求,所以下面对需要调整的经以展示!
{
	if(hei==2) return "1";
	else if(hei == 3) return "7";
	else if(hei == 4) return "4";
	else if(hei == 5) return "2";
	else if(hei == 6) return "6";
	else if(hei == 7) return "8";
	else
	{
		if(hei-7 == 1) return creatmin(2)+"0";
		if(hei-7 == 3) return creatmin(5)+"2";
		if(hei-7 == 4) return creatmin(5)+"0";
		if(hei -7 == 6) return "68";
		if(hei == 17) return "200";
		if(hei == 18) return "208";
		if(hei == 19) return "288";
		if(hei == 20) return "688";
		return creatmin(hei-7)+"8";
	}
}
string creat(int hei,int len)//搜索测试。。。
{
	
	if(hei==2) return "1";
	else if(hei == 3) return "7";
	else if(hei == 4) return "4";
	else if(hei == 5) return "2";
	else if(hei == 6) return "6";
	else if(hei == 7) return "8";
	if(hei == 0 || hei == 1) {return "";}
	if(len>=L || (hei%7==0?hei/7:hei/7+1)>L-len) {return "";}
	{
		string s="";
		for(int i=9;i>=0;i--)
		{
			string ss;
			if(i!=0 || (i==0 && hei - 6!=0))
 			   ss = creat(hei-ar[i],len+1);
			else continue;
			if(ss=="") continue;
			else {ss += (char)(i+'0'); if(s=="") s = ss;else if(s.compare(ss)>0) s = ss;}
		}
		return s;
	}
}
string creatmax(int hei)//创建最大值,比较简单,实际就是奇偶性
{
	if(hei ==2) return "1";
	if(hei -2 == 1) return "7";
	return creatmax(hei-2)+"1";
}
void init()
{
	int test;
	PF(test);
	while(test--)
	{
		int n;
		PF(n);
		cout<<creatmin(n)<<" "<<creatmax(n)<<endl;

	}
	return ;
}
int main()
{
	init();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值