藍橋杯-算式成立的數量

題目

___B___DEF
A+—±——— = 10
___C___GHI

(其中下劃線只是爲了對齊顯示,如果顯示有問題,可到qq上查看圖形)
這個算式中AI代表19的數字,不同的字母代表不同的數字。
比如:
6+8/3+952/714 就是一種解法,
5+3/1+972/486 是另一種解法。
這個算式一共有多少種解法?
注意:你提交應該是個整數,不要填寫任何多餘的內容或說明性文字。

代碼

#include <bits/stdc++.h>
using namespace std;
int cnt, x[9], vis[10];
void dfs(int step){
	if(step == 9){
		int a = x[0], b = x[1], c = x[2];
		int def = x[3]*100 + x[4]*10 + x[5];
		int ghi = x[6]*100 + x[7]*10 + x[8];
		if(a*c*ghi + b*ghi + c*def == 10*c*ghi){
			cnt++;
			//printf("%d+%d/%d+%d/%d=10\n", a, b, c, def, ghi); 
		} 
	}
	for(int i = 1; i<10; i++){
		if(i==0&&(step == 2||step == 3||step == 6)) continue;
		if(vis[i]) continue;
		vis[i] = 1;
		x[step] = i;
		dfs(step+1);
		vis[i] = 0;
	} 
}
int main(){
	cnt = 0; 
	memset(vis, 0, sizeof(vis));
	dfs(0);
	printf("%d", cnt);
	return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章