湖南第一师范原创题,比较好玩.
E- Seven tombsTime Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)Total Submission(s) : 31 Accepted Submission(s) : 9Font: Times New Roman | Verdana | GeorgiaFont Size: ← →Problem Description塔.拉夏被埋葬在术士峡谷的七个古墓中的一个。
塔.拉夏的古墓一共有七种不同的符号,分别用A、B、C、D、E、F、G表示。每个古墓中分别封印着一种力量,用1、2、3、4、5、6、7表示。为了防止以后有人取得这些力量,赫拉迪姆将这些对应的关系全部隐藏起来了。
为了获得这七种力量,你终于找到了一点线索:一个古代赫拉迪姆留下的式子。这个式子表示了七种力量对应的关系。经过破译,终于知道将七种符号所表示的力量的代号分别代进式子中,使得等式成立的,就是开启封印的钥匙。
写一个程序解开这个迷题。
Input一行一个字符串,为一个只有变量A..G的等式。式子中只含有字母、加、减、乘号以及括号和一个等号,并且“ABC”表示A*100+B*10+C,字符串长度不超过100。Output一行,输出对应等式的解。如果有多种可能,输出ABCDEFG表示十进制数最小的一个。输入数据保证有解。Sample Input(A+B)*C-E*(C+D)=FF
Sample Output(2+6)*7-1*(7+5)=44
Authorhnfnu
枚举7^5~很好想到~效率也能接受~关键就是解析算数式~先将括号的情况处理了~用递归的思路~~很简单~~而乘法的优先级问题~我没多想~~干脆每个乘法两侧加括号~~优先级就上来了~
Program:#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;}
Total Submission(s) : 31 Accepted Submission(s) : 9Font: Times New Roman | Verdana | GeorgiaFont Size: ← →Problem Description塔.拉夏被埋葬在术士峡谷的七个古墓中的一个。
塔.拉夏的古墓一共有七种不同的符号,分别用A、B、C、D、E、F、G表示。每个古墓中分别封印着一种力量,用1、2、3、4、5、6、7表示。为了防止以后有人取得这些力量,赫拉迪姆将这些对应的关系全部隐藏起来了。
为了获得这七种力量,你终于找到了一点线索:一个古代赫拉迪姆留下的式子。这个式子表示了七种力量对应的关系。经过破译,终于知道将七种符号所表示的力量的代号分别代进式子中,使得等式成立的,就是开启封印的钥匙。
写一个程序解开这个迷题。
Input一行一个字符串,为一个只有变量A..G的等式。式子中只含有字母、加、减、乘号以及括号和一个等号,并且“ABC”表示A*100+B*10+C,字符串长度不超过100。Output一行,输出对应等式的解。如果有多种可能,输出ABCDEFG表示十进制数最小的一个。输入数据保证有解。Sample Input(A+B)*C-E*(C+D)=FF
Sample Output(2+6)*7-1*(7+5)=44
Authorhnfnu
枚举7^5~很好想到~效率也能接受~关键就是解析算数式~先将括号的情况处理了~用递归的思路~~很简单~~而乘法的优先级问题~我没多想~~干脆每个乘法两侧加括号~~优先级就上来了~
Program:#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;}
Font Size: ← →Problem Description塔.拉夏被埋葬在术士峡谷的七个古墓中的一个。
塔.拉夏的古墓一共有七种不同的符号,分别用A、B、C、D、E、F、G表示。每个古墓中分别封印着一种力量,用1、2、3、4、5、6、7表示。为了防止以后有人取得这些力量,赫拉迪姆将这些对应的关系全部隐藏起来了。
为了获得这七种力量,你终于找到了一点线索:一个古代赫拉迪姆留下的式子。这个式子表示了七种力量对应的关系。经过破译,终于知道将七种符号所表示的力量的代号分别代进式子中,使得等式成立的,就是开启封印的钥匙。
写一个程序解开这个迷题。
Input一行一个字符串,为一个只有变量A..G的等式。式子中只含有字母、加、减、乘号以及括号和一个等号,并且“ABC”表示A*100+B*10+C,字符串长度不超过100。Output一行,输出对应等式的解。如果有多种可能,输出ABCDEFG表示十进制数最小的一个。输入数据保证有解。Sample Input(A+B)*C-E*(C+D)=FF
Sample Output(2+6)*7-1*(7+5)=44
Authorhnfnu
枚举7^5~很好想到~效率也能接受~关键就是解析算数式~先将括号的情况处理了~用递归的思路~~很简单~~而乘法的优先级问题~我没多想~~干脆每个乘法两侧加括号~~优先级就上来了~
Program:#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;}
塔.拉夏的古墓一共有七种不同的符号,分别用A、B、C、D、E、F、G表示。每个古墓中分别封印着一种力量,用1、2、3、4、5、6、7表示。为了防止以后有人取得这些力量,赫拉迪姆将这些对应的关系全部隐藏起来了。
为了获得这七种力量,你终于找到了一点线索:一个古代赫拉迪姆留下的式子。这个式子表示了七种力量对应的关系。经过破译,终于知道将七种符号所表示的力量的代号分别代进式子中,使得等式成立的,就是开启封印的钥匙。
写一个程序解开这个迷题。
Input一行一个字符串,为一个只有变量A..G的等式。式子中只含有字母、加、减、乘号以及括号和一个等号,并且“ABC”表示A*100+B*10+C,字符串长度不超过100。Output一行,输出对应等式的解。如果有多种可能,输出ABCDEFG表示十进制数最小的一个。输入数据保证有解。Sample Input(A+B)*C-E*(C+D)=FF
Sample Output(2+6)*7-1*(7+5)=44
Authorhnfnu
枚举7^5~很好想到~效率也能接受~关键就是解析算数式~先将括号的情况处理了~用递归的思路~~很简单~~而乘法的优先级问题~我没多想~~干脆每个乘法两侧加括号~~优先级就上来了~
Program:#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;}
Sample Input(A+B)*C-E*(C+D)=FF
Sample Output(2+6)*7-1*(7+5)=44
Authorhnfnu
枚举7^5~很好想到~效率也能接受~关键就是解析算数式~先将括号的情况处理了~用递归的思路~~很简单~~而乘法的优先级问题~我没多想~~干脆每个乘法两侧加括号~~优先级就上来了~
Program:#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;}
(2+6)*7-1*(7+5)=44