Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
GCC提交WA,C提交RE,我汗~~# include <stdio.h> # include <string.h> # include <stdlib.h> typedef struct { int p; char c; }sig; int total; int eq[100],len; __int64 cul(__int64 a,__int64 b,char c) { if(c=='+') return a+b; else if(c=='-') return a-b; else return a*b; } int chk() { sig stack2[50]; __int64 stack1[50]; int top1=-1,top2=-1,i,p=1; for(i=1;i<=len;i++) { if(eq[i]=='(') p++; else if(eq[i]==')') { p--; stack1[top1-1]=cul(stack1[top1-1],stack1[top1],stack2[top2].c); top1--; top2--; } else if(eq[i]=='-'||eq[i]=='+'||eq[i]=='*') { if(top2==-1||p>stack2[top2].p) { stack2[++top2].p=p; stack2[top2].c=(char)eq[i]; } else { stack1[top1-1]=cul(stack1[top1-1],stack1[top1],stack2[top2].c); top1--; stack2[top2].c=(char)eq[i]; stack2[top2].p=p; } } else stack1[++top1]=eq[i]; } if(top1) stack1[top1-1]=cul(stack1[top1-1],stack1[top1],stack2[top2].c); if(stack1[0]==total) return 1; else return 0; } int det(int num) { if(num>len) { return chk(); } else if(eq[num]!=' ') return det(num+1); else { eq[num]='+'; if(det(num+1)) return 1; eq[num]='-'; if(det(num+1)) return 1; eq[num]='*'; if(det(num+1)) return 1; return 0; } } int main() { int casenum=1; while(1) { char temp[1000],*t; int i; scanf("%d",&total); if(!total) break; scanf(" = "); gets(temp); printf("Equation #%d:\n",casenum++); len=0; t=strtok(temp," "); while(t!=NULL) { if(t[0]=='(') { eq[++len]='('; t=t+1; } eq[++len]=atoi(t); if(t[strlen(t)-1]==')') { eq[++len]=')'; t[strlen(t)-1]='\0'; } eq[++len]=' '; t=strtok(NULL," "); } len--; if(det(1)) { printf("%d=",total); for(i=1;i<=len;i++) { if(eq[i]!='+'&&eq[i]!='-'&&eq[i]!='*'&&eq[i]!='('&&eq[i]!=')') printf("%d",eq[i]); else printf("%c",eq[i]); } printf("\n"); } else printf("Impossible\n"); printf("\n"); } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator