Examples Summary - Count 24 points

A topic

· Title Contents :

1.Background
CZR liked mathematics, but mathematics he has been bad, so he decided to play 24 points to practice
their own .
2.Description
However, due to his bad math, so he would only add and subtract:
given four numbers, I ask whether through addition and subtraction to get 24 points .
In order to train himself, he always will be 5 Ge 24 -point game
- topic source : Shandong training exams two second title

· Algorithms : Violence / dfs

· Too simple (I do not even feel too weak konjac), not introduced, so a waste of mind Gangster

· Board a : [violence] 

#include <cstdio>

using namespace std;

int main() {
	//freopen("24.in", "r", stdin);
	//freopen("24.out", "w", stdout);
	int p[5][2];
	int tmp;
	int a[5];
	
	p[1][0] = p[2][0] = p[3][0] = p[4][0] = -1;
	p[1][1] = p[2][1] = p[3][1] = p[4][1] = 1;
	
	for (int T = 1; T <= 5; ++T) {
		for (int i = 1; i <= 4; ++i)
			scanf("%d", a + i);
		for (int p1 = 0; p1 <= 1; ++p1)
		for (int p2 = 0; p2 <= 1; ++p2)
		for (int p3 = 0; p3 <= 1; ++p3)
		for (int p4 = 0; p4 <= 1; ++p4)	{
			tmp = p[1][p1] * a[1] + p[2][p2] * a[2] + p[3][p3] * a[3] + p[4][p4] * a[4];
			if (tmp == 24) {
				puts("Yes.");
				goto end;
			}
		}
		puts("No.");
		end: ;
	}
	//fclose(stdin);
	//fclose(stdout);
	return 0;
}

Board two : [dfs]

#include<iostream>
#include<cstdio>
using namespace std;
int x[5];
bool flag=false;
void search(int a,int index,int ans)
{
    if(index==4&&ans==24) flag=true;
	if(index>4) return ;
	search(x[index+1],index+1,ans+a);
	search(-x[index+1],index+1,ans+a);
 } 
int main()
{
	//freoepn("24.in","r",stdin);
	//freopen("24.out","w",stdout);
	for(int i=1;i<=5;i++)
	{
		flag=false;
		cin>>x[1]>>x[2]>>x[3]>>x[4];
		search(x[1],1,0);
		search(-x[1],1,0);
		if(flag) cout<<"Yes."<<endl;
		else cout<<"No."<<endl;
	} 
	//fclose(stdin);
	//fclose(stdout); 
	return 0;
}

Topic two

· Title Contents :

 

Guess you like

Origin www.cnblogs.com/konglingyi/p/11391035.html