Three examples of typical enumeration algorithm

Copyright: from: Jihome https://blog.csdn.net/jihome/article/details/89162549

1. fuzzy numbers

Problem: A number five, ten thousand and one hundred do not know, but it is a multiple of 57 and 67, all possibilities to find.
Analysis: direct enumeration of all cases, the judge can.

#include <iostream>
using namespace std;
int main(){
	int w,q,b,s,g;
	int sum,i,count=0,j;
	int n[10];
	cin>>q>>s>>g;
	while((q!=-1)||(s!=-1)||(g!=-1)){
	
		sum=q*1000+s*10+g;
		for(j=1;j<10;j++) {		
		for(i=0;i<10;i++){
			sum=sum+i*100+j*10000;
			if(sum%57==0&&sum%67==0)
			{
				n[count++]=sum;
			}
		}
	}
		cout<<count<<" ";
		for(i=0;i<count;i++){
			cout<<n[i]<<" ";
		}
		count=0;
		cout<<endl;
		cin>>w>>q>>s>>g;
	};
	return 0;
} 

N 2.m money to buy chicken problem

Question: 5 males, 3 hens, three chickens 1 to obtain m n money chickens of all possible solutions.
Analysis: Violence solved triple loop (loop can be simplified into a double).

#include <iostream>
using namespace std;
int main(){
	int m,n,count=0;
	int i,j,k;
	Maybe M[100];
	cin>>m>>n;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			for(k=0;k<n;k++){
				if((i+k+j==n)&&(i*5+j*3+k/3.0==m)){
					count++;
				}
			}
		}
	}
	cout<<count<<endl;
	return 0;
} 

Improvement:
K Save directly with the total number of two, and then determine, at least one cycle. Operation efficiency is greatly improved.

3. genuine silver

Question: 12 silver coins, a fake, do not know the severity of counterfeit money, to three tests, which ask counterfeit money, real money or heavier than light.
Analysis: reverse thinking, with each coin test between true and false, the condition can be.

#include <iostream>
#include <cstring>
using namespace std;
bool isHeavy(char );
bool isLight(char );

char Left[3][10],Right[3][10],Than[3][10];

int main(){
	int n,i;
	char c;
	cin>>n;
	while(n--){
		for(i=0;i<3;i++){
			cin>>Left[i]>>Right[i]>>Than[i];
		}
			for(c='A';c<='L';c++){
				if(isLight(c)){
					cout<<c<<" "<<"light"<<endl;
					break;
				}
				if(isHeavy(c)){
					cout<<c<<" "<<"heavy"<<endl;
					break;
				}
			}
	}
	return 0;
} 

bool isHeavy(char c){
	for(int i=0;i<3;i++){
		switch(Than[i][0]){
			case 'u':
				if(strchr(Left[i],c)==NULL)
					return false;break;
			case 'e':
				if((strchr(Left[i],c)!=NULL)||(strchr(Right[i],c)!=NULL))
					return false;break;
			case 'd':
				if(strchr(Right[i],c)==NULL)
					return false;break;
		}
	}
	return true;
}

bool isLight(char c){
	for(int i=0;i<3;i++){
		switch(Than[i][0]){
			case 'u':
				if(strchr(Right[i],c)==NULL)
					return false;break;
			case 'e':
				if((strchr(Left[i],c)!=NULL)||(strchr(Right[i],c)!=NULL))
					return false;break;
			case 'd':
				if(strchr(Left[i],c)==NULL)
					return false;break;
		}
	}
	return true;
}

Pit Warning: use of DevC ++, I do not know why char define left, right, than the first letter is not lowercase, uppercase after not being given.
Determine the severity of the time switch inside do not forget the break.

Guess you like

Origin blog.csdn.net/jihome/article/details/89162549