2016蓝桥杯国赛凑平方数题解

蓝桥杯凑平方数题解


1.题目:
把0~9这10个数字,分成多个组,每个组恰好是一个平方数,这是能够办到的。
比如:0, 36, 5948721
再比如:
1098524736
1, 25, 6390784
0, 4, 289, 15376
等等…

2.思路:
9876543210是能凑的最大数,所以将其开根得到一个数m。从0-m找到所有无重复位的数,并将其转化成string,记录下来。
之后利用递归思想,把组合数和为一个字符串,用判重函数check检验是否成立,记录答案即可。(ps:做这个题一定要细心,我就是因为不细心错了好多回 ToT )
3.c++代码

#include<bits/stdc++.h>
using namespace std;
#define MAX 9876543210
#define ll long long
string t[99380];//存所有无重复位的平方数
ll k=0;
int ans=0;
int check(string n)//检验是否有重复
{
    ll co[10]={0};
	int j=0,tt;
	int count=0;
	fill(co,co+10,0);
	for(j=0;j<n.length();j++)
	{
		if(n[j]==' ')continue;
		count++;
		tt=n[j]-'0';
		co[tt]++;
		if(co[tt]>1)return 0;//有重复返回0
	}	
	return count;//无重复返回位数
}
void fun(string n,int j)
{
	int i;
	if(!check(n))return ;	
	if(check(n)==10)//
	{
		ans++;
		cout<<ans<<": "<<n<<endl;
		
		return;
	}
	for(i=j+1;i<k;i++)
	{
		fun(n+" "+t[i],i);
	}
}
string tostr(ll n)
{
	string t1="",t2="";
	ll te;
	if(n==0)return t1+'0';
	while(n)
	{
		te=n%10;
		n/=10;
		t1+=(te+'0');
	}
	for(int i=t1.length()-1;i>=0;i--)
	{
		t2+=t1[i];
	}
	return t2;
	
}
int main()
{	
	ll i,j;
	ll d=993800;
	cout<<d;
	string tstr;
	for( i=0;i<=d;i++)
	{
		tstr=tostr(i*i);	
		if(check(tstr)) 
		{
			t[k++]=tstr;	 
			cout<<tstr<<endl;
		 } 
	}
	for(j=0;j<k;j++)
	{
		fun(t[j],j);
	}
	printf("ans:%d ",ans);

 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值