用c语言编写21点小游戏
用c语言编写21点小游戏
◀◀◀点击图片进入游戏————◀◀◀◀———点击图片进入游戏———真人百家乐,百家乐技巧,真钱游戏
#include
#include
#include
using namespace std;
const double PRECISION=1E-6;
const int COUNT_OF_NUMBER=4;
const int NUMBER_TO_BE_CAL=21;
double number[COUNT_OF_NUMBER];
string expression[COUNT_OF_NUMBER];
bool Search(int n)
{
if(n==1)
{
if(fabs(number[0]-NUMBER_TO_BE_CAL) { cout< return true; } else return false; } for(int i=0;i { for(int j=i+1;j { double a,b; string expa,expb; a=number[i]; b=number[j]; number[j]=number[n-1]; expa=expression[i]; expb=expression[j]; expression[j]=expression[n-1]; expression[i]=(+expa+++expb+); number[i]=a+b; if(Search(n-1)) return true; expression[i]=(+expa+-+expb+); number[i]=a-b; if(Search(n-1)) return true; expression[i]=(+expb+-+expa+); number[i]=b-a; if(Search(n-1)) return true; expression[i]=(+expa+*+expb+); number[i]=a*b; if(Search(n-1)) return true; if(b!=0) { expression[i]=(+expa+/+expb+); number[i]=a/b; if(Search(n-1)) return true; } if(a!=0) { expression[i]=(+expb+/+expa+); number[i]=b/a; if(Search(n-1)) return true; } number[i]=a; number[j]=b; expression[i]=expa; expression[j]=expb; } } return false; } void main() { for(int i=0;i { char buffer[20]; int x; cin>>x; number[i]=x; itoa(x,buffer,10); expression[i]=buffer; } if(Search(COUNT_OF_NUMBER)) cout<<"Success."< else cout<<"Fail."< } 这里有个代码,不过如果你是初学者,我个人建议你还是先把书本搞透再说,这个对初学者有点复杂的。 用c语言编写21点小游戏
Comments
Post a Comment