三羊献瑞-第六届蓝桥杯省赛

三羊献瑞

观察下面的加法算式:

  祥 瑞 生 辉
  • 三 羊 献 瑞

三 羊 生 瑞 气

(如果有对齐问题,可以参看【图1.jpg】)

其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。

请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。

暴力解题
设abce + efgb = efcbh
遍历得解
答案:1085
因为没有判定两个加数中是否有相同数字的 , 所以输出有多组, 但比对一下即可找到答案
在这里插入图片描述

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

int num[100];

bool havesame(int a, int b, int c, int d)
{
	if(a == b || a == c || a == d || b == c || b == d || c == d)
	return true;
	return false;
}

int main()
{
	for(int a = 1; a < 10; a++)
	for(int b = 0; b < 10; b++)
	for(int c = 0; c < 10; c++)
	for(int d = 0; d < 10; d++)
	{
		if(havesame(a, b, c, d)) continue;
		int num1 = a * 1000 + b * 100 + c * 10 + d;
		for(int e = 1; e < 10; e++)
		for(int f = 0; f < 10; f++)
		for(int g = 0; g < 10; g++)
		{
			if(havesame(e, f, g, b)) continue;
			int num2 = e * 1000 + f * 100 + g * 10 + b;
			int num3 = num1 + num2;
			int t = num3 % 10;
			if(t == a || t == b || t == c || t == d || t == e || t == f || t == g)
			continue;
			num3 /= 10;
			int num4 = e * 1000 + f * 100 + c * 10 + b;
			if(num3 == num4)
			{
				cout << num1 << endl;
				cout << num2 << endl;
				cout << num3 << endl << endl;
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40212930/article/details/88673937