0
点赞
收藏
分享

微信扫一扫

猜字谜


元宵节灯会上有一迷题:
        请猜谜 * 请猜谜 = 请边赏灯边猜
    小明想,一定是每个汉字代表一个数字,不同的汉字代表不同的数字。
    请你用计算机按小明的思路算一下,然后提交“请猜谜”三个字所代表的整数即可。
    请严格按照格式,通过浏览器提交答案。
    注意:只提交一个3位的整数,不要写其它附加内容,比如:说明性的文字。


思路一: 暴力。


#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
	for(int a=1;a<=9;a++)
	for(int b=0;b<=9;b++)
	for(int c=0;c<=9;c++)
	for(int d=0;d<=9;d++)
	for(int e=0;e<=9;e++)
	for(int f=0;f<=9;f++)
		{
			int m=a*100+b*10+c;
			int n=a*100000+d*10000+e*1000+f*100+d*10+b;
			if(m*m==n)
				cout<<m<<" ";
		}
	return 0;
}


思路二:dfs

#include<algorithm>
#include<cstdio>  
using namespace std;
int a[6] = {0};
void dfs(int x)
{
	int i;
	if (a[0] == 0 && x>0)
		return ;
	if (x>6)
		return ;
	else if (x==6)
		{
			if ((a[0]*100+a[1]*10+a[2]) * (a[0]*100+a[1]*10+a[2]) == a[0]*100000+a[3]*10000+a[4]*1000+a[5]*100+a[3]*10+a[1])
			{
				printf("%d%d%d",a[0],a[1],a[2]);
			}
			return ;
		}
	for (i=0; i<=9; i++)
	{
		a[x] = i;
		dfs(x+1);
	}
}
int main()
{
	dfs(0);
	return 0;
}





举报

相关推荐

0 条评论