湖南第一师范原创题,比较好玩.
(A+B)*C-E*(C+D)=FF
(2+6)*7-1*(7+5)=44
#include<iostream>#include<string.h>#include<math.h>#include<stdio.h>#include<queue>#include<algorithm>#define ll long longusing namespace std;ll a[8],i,j,l,p,S1,S2,stack;char s[120],str[300];ll getnum(){ ll x,y,t; while (p<l && s[p]!='=' && s[p]!=')') { if (s[p]=='(') { p++; x=getnum(); }else if (s[p]>='A' && s[p]<='F') { x=0; while (s[p]>='A' && s[p]<='F') { x=x*10+a[s[p]-'A']; p++; } } t=-1; if (s[p]=='+') t=0; else if (s[p]=='-') t=1; else if (s[p]=='*') t=2; else p--; p++; if (s[p]=='(') { p++; y=getnum(); }else { y=0; while (s[p]>='A' && s[p]<='F') { y=y*10+a[s[p]-'A']; p++; } } if (t==0) x+=y; else if (t==1) x-=y; else if (t==2) x*=y; } p++; return x;}bool judge(){ p=0; S1=getnum(); S2=getnum(); if (S1==S2) return true; return false;}int main(){ freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); gets(s); strcpy(str,s); l=strlen(s); i=0; while (1) { for (;i<l;i++) if (s[i]=='*') break; if (i==l) break; j=i+1; stack=0; if (s[j]=='(') stack=1; while (stack) { j++; if (s[j]==')') stack--; if (s[j]=='(') stack++; } j++; while (s[j]>='A' && s[j]<='F') j++; for (p=l;p>j;p--) s[p]=s[p-1]; s[j]=')'; l++; j=i-1; stack=0; if (s[j]==')') stack=1; while (stack) { j--; if (s[j]==')') stack++; if (s[j]=='(') stack--; } while (s[j]>='A' && s[j]<='F') j--; j++; for (p=l;p>j;p--) s[p]=s[p-1]; s[j]='('; l++; s[l]='\0'; i+=2; } // puts(s); for (a[0]=1;a[0]<=7;a[0]++) for (a[1]=1;a[1]<=7;a[1]++) if (a[1]!=a[0]) for (a[2]=1;a[2]<=7;a[2]++) if (a[2]!=a[1] && a[2]!=a[0]) for (a[3]=1;a[3]<=7;a[3]++) if (a[3]!=a[2] && a[3]!=a[1] && a[3]!=a[0]) for (a[4]=1;a[4]<=7;a[4]++) if (a[4]!=a[3] && a[4]!=a[2] && a[4]!=a[1] && a[4]!=a[0]) for (a[5]=1;a[5]<=7;a[5]++) if (a[5]!=a[4] && a[5]!=a[3] && a[5]!=a[2] && a[5]!=a[1] && a[5]!=a[0]) if (judge()) goto A; A: ; for (i=0;i<l;i++) if (str[i]>='A' && str[i]<='F') printf("%I64d",a[str[i]-'A']); else printf("%c",str[i]); printf("\n"); // judge(); return 0;}