• 个人简介

    #include<bits/stdc++.h>
    using namespace std;
    struct Item{
    	int npo;
    	double num;
    	char op;
    };
    string s;
    char op_stack[1005];
    Item st[1005];
    double ans[1005];
    int top1, top2;
    int pri(char ch){
    	if(ch=='*'||ch=='/') return 2;
    	if(ch=='+'||ch=='-') return 1;
    	return 0;
    }
    double lk(string str){
    	top1=top2=0;
    	for(int i=0;i<(int)str.size();i++){
    		if(isdigit(str[i])||str[i]=='.'){
    			double x=0,f=0;
    			int fl=0;
    			bool isf=0;
    			while(i<(int)str.size()&&(isdigit(str[i])||str[i]=='.')){
    				if(str[i]=='.'){
    					isf=1;
    					i++;
    					continue;
    				}
    				if(!isf) x=x*10+(str[i]-'0');
    				else{
    					f=f*10+(str[i]-'0');
    					fl++;
    				}
    				i++;
    			}
    			i--;
    			if(isf) x=x+f/pow(10.0,fl);
    			st[++top1].npo=0;
    			st[top1].num=x;
    		}
    		else if(str[i]=='(') op_stack[++top2]=str[i];
    		else if(str[i]==')'){
    			while(top2&&op_stack[top2]!='('){
    				st[++top1].npo=1;
    				st[top1].op=op_stack[top2--];
    			}
    			top2--;
    		}
    		else if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'){
    			while(top2&&op_stack[top2]!='('&&pri(op_stack[top2])>=pri(str[i])){
    				st[++top1].npo=1;
    				st[top1].op=op_stack[top2--];
    			}
    			op_stack[++top2]=str[i];
    		}
    	}
    	while(top2){
    		st[++top1].npo=1;
    		st[top1].op=op_stack[top2--];
    	}
    	int t=top1;
    	top1=0;
    	for(int i=1;i<=t;i++){
    		if(st[i].npo==0) ans[++top1]=st[i].num;
    		else{
    			double b=ans[top1--];
    			double a=ans[top1--];
    			char ch=st[i].op;
    			if(ch=='+') ans[++top1]=a+b;
    			else if(ch=='-') ans[++top1]=a-b;
    			else if(ch=='*') ans[++top1]=a*b;
    			else if(ch=='/') ans[++top1]=a/b;
    		}
    	}
    	return ans[1];
    }
    bool checkNumbers(string str,int num[4]){
    	double fd[10];
    	int cnt=0;
    	for(int i=0;i<(int)str.size();i++){
    		if(isdigit(str[i])||str[i]=='.'){
    			double x=0,f=0;
    			int fl=0;
    			bool isf=0;
    			while(i<(int)str.size()&&(isdigit(str[i])||str[i]=='.')){
    				if(str[i]=='.'){
    					isf=1;
    					i++;
    					continue;
    				}
    				if(!isf) x=x*10+(str[i]-'0');
    				else{
    					f=f*10+(str[i]-'0');
    					fl++;
    				}
    				i++;
    			}
    			i--;
    			if(isf) x=x+f/pow(10.0,fl);
    			fd[cnt++]=x;
    		}
    	}
    	if(cnt!=4) return false;
    	double tmp[4];
    	for(int i=0;i<4;i++) tmp[i]=fd[i];
    	sort(tmp,tmp+4);
    	double nd[4];
    	for(int i=0;i<4;i++) nd[i]=num[i];
    	sort(nd,nd+4);
    	for(int i=0;i<4;i++) if(fabs(tmp[i]-nd[i])>1e-6) return false;
    	return true;
    }
    bool dfs(double a[4],string e[4],int step,string&res){
    	if(step==1){
    		if(fabs(a[0]-24)<1e-6){
    			res=e[0];
    			return true;
    		}
    		return false;
    	}
    	for(int i=0;i<step;i++){
    		for(int j=0;j<step;j++){
    			if(i==j) continue;
    			double b[4];
    			string be[4];
    			int cnt=0;
    			for(int k=0;k<step;k++){
    				if(k!=i&&k!=j){
    					b[cnt]=a[k];
    					be[cnt]=e[k];
    					cnt++;
    				}
    			}
    			double x=a[i],y=a[j];
    			b[cnt]=x+y;
    			be[cnt]="("+e[i]+"+"+e[j]+")";
    			if(dfs(b,be,step-1,res)) return true;
    			b[cnt]=x-y;
    			be[cnt]="("+e[i]+"-"+e[j]+")";
    			if(dfs(b,be,step-1,res)) return true;
    			b[cnt]=x*y;
    			be[cnt]="("+e[i]+"*"+e[j]+")";
    			if(dfs(b,be,step-1,res)) return true;
    			if(fabs(y)>1e-6){
    				b[cnt]=x/y;
    				be[cnt]="("+e[i]+"/"+e[j]+")";
    				if(dfs(b,be,step-1,res)) return true;
    			}
    		}
    	}
    	return false;
    }
    string getSolution(int num[4]){
    	double a[4];
    	for(int i=0;i<4;i++) a[i]=num[i];
    	sort(a,a+4);
    	do{
    		double cur[4];
    		string cure[4];
    		for(int i=0;i<4;i++){
    			cur[i]=a[i];
    			char buf[10];
    			sprintf(buf,"%d",(int)(cur[i]+0.5));
    			cure[i]=buf;
    		}
    		string res;
    		if(dfs(cur,cure,4,res)) return res;
    	}while(next_permutation(a,a+4));
    	return "";
    }
    int main(){
    	srand(time(0));
    	int num[4];
    	string sol;
    	do{
    		for(int i=0;i<4;i++) num[i]=rand()%13+1;
    		sol=getSolution(num);
    	}while(sol=="");
    	cout<<"四个数字:";
    	for(int i=0;i<4;i++) cout<<num[i]<<" ";
    	cout<<endl;
    	cout<<"请使用 + - * / 和括号构造表达式,使结果等于24,每个数字用一次。"<<endl;
    	cout<<"输入表达式(或输入“赵冠勋牛逼”查看答案,输入“dogneedshelp”获取提示):"<<endl;
    	while(true){
    		cout<<"> ";
    		getline(cin,s);
    		for(size_t i=0;i<s.length();i++){
    			if(s[i]=='(') s[i]='(';
    			if(s[i]==')') s[i]=')';
    		}
    		if(s=="赵冠勋牛逼"){
    			cout<<"一个可行的表达式是:"<<sol<<endl;
    			continue;
    		}
    		if(s=="dogneedshelp"){
    			cout<<"就不告诉你,嘻嘻~"<<endl;
    			continue;
    		}
    		string cl;
    		for(int i=0;i<(int)s.size();i++){
    			if(s[i]!=' ') cl.push_back(s[i]);
    		}
    		if(!checkNumbers(cl,num)){
    			cout<<"错误:表达式未使用给定的四个数字各一次!请重试。"<<endl;
    			continue;
    		}
    		double r=lk(cl);
    		cout<<"计算结果:"<<r<<endl;
    		if(fabs(r-24)<1e-6){
    			cout<<"恭喜!正确!"<<endl;
    			cout<<"“记得给一个积分呕~”"; 
    			for(int z=1;z<=3;z++){
    				for(int i=1;i<=1e8;i++){
    					;
    				}
    				system("color 24");
    				for(int i=1;i<=1e8;i++){
    					;
    				}
    				system("color 46");
    				for(int i=1;i<=1e8;i++){
    					;
    				}
    				system("color 6A");
    			}
    			break;
    		}
    		else{
    			cout<<"可惜,结果不是24 请重试 "<<endl;
    		}
    	}
    	return 0;
    }
    
  • 最近活动