2016年第七届蓝桥杯C/C++程序设计本科B组省赛 凑算式(结果填空) DFS

//A + B/C + DEF/GHI = 10 凑算式 标记一下1-9个数,再搜索一下就好了
#include <iostream>
using namespace std;
bool visited[10];
int ans[100];
int k = 0;
int coun = 0;

bool judge(){
	double x = (double)ans[1]/ans[2];
	double y = (double)(ans[3]*100+ans[4]*10+ans[5])/(ans[6]*100+ans[7]*10+ans[8]);
	
	if(ans[0]+x+y == 10)
		return 1;
	else
		return 0;
}

void dfs(int depth){
	if(depth >= 9)
	{
		if(judge())
			coun++;
		return;
	}		
	for(int i = 1; i < 10; i++)
		if(!visited[i])
		{
			visited[i] = true;
			ans[k++] = i;
			dfs(depth+1);
			visited[i] = false;
			k--;
		}
}

int main(){
	dfs(0);	
	cout << coun << endl;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_37430374/article/details/79663024