您的位置:首页 > 其它

蓝桥杯:马虎的算式(非递归)

2014-03-19 21:12 295 查看
标题: 马虎的算式

    小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。

    有一次,老师出的题目是:36 x 495 = ?

    他却给抄成了:396 x 45 = ?

    但结果却很戏剧性,他的答案竟然是对的!!

    因为 36 * 495 = 396 * 45 = 17820

    类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54

    假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)

    能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢?

请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。

满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。
#include <cstdio>
#include <cstring>
using namespace std;

int judge(int *s)
{
	int a[10];
	memset(a, 0, sizeof(a));
	for(int i=0; i < 5; i++) 
	{
		if(a[s[i]]==1) return 0;
		else a[s[i]]=1;
	}
	return 1;
}
int main()
{
	int n, m, count=0;
	for(n=12; n<99; n++)
	{
		for(m=100; m<999; m++)
		{
			int a, b, c, d, e, adb, ce;
			int arr[5];
			memset(arr, 0, sizeof(arr));
			a=n/10;arr[0]=a;
			b=n%10;arr[1]=b;
			c=m/100;arr[2]=c;
			d=m%100/10;arr[3]=d;
			e=m%10;arr[4]=e;
			if(a&&b&&c&&d&&e&&judge(arr)) 
			{
				adb = a*100+d*10+b;
				ce = c*10+e;
				if(m*n == adb*ce)
					count++;
			}
		}
	}
	printf("%d\n", count);
	
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: