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 |
Re:附上代码,WA了5次,一直怀疑算法,原来是每次开始没有初始化,数据,所以大家要有好习惯哈。140k,0msIn Reply To:附上代码,WA了5次,一直怀疑算法,原来是每次开始没有初始化,数据,所以大家要有好习惯哈。140k,0ms Posted by:scuwf at 2011-09-26 21:55:16 > #include<stdio.h> > #include<string.h> > #define MAXSIZE 6 > typedef struct > { > int trcoin; > int light; > int heavy; > }Coin; > int value(char result[]) > { > if(strcmp("even",result)==0) return 0; > if(strcmp("up",result)==0) return 1; > return 2; > } > void hand(char left[],char right[],int value,Coin coin[]) > { > unsigned int i; > if(value==0) > { > for(i=0;i<strlen(left);i++) > { > coin[left[i]-65].trcoin=1; > } > for(i=0;i<strlen(right);i++) > { > coin[right[i]-65].trcoin=1; > } > } > else if(value==1) > { > for(i=0;i<strlen(left);i++) > { > coin[left[i]-65].heavy++; > } > for(i=0;i<strlen(right);i++) > { > coin[right[i]-65].light++; > } > } > else > { > for(i=0;i<strlen(left);i++) > { > coin[left[i]-65].light++; > } > for(i=0;i<strlen(right);i++) > { > coin[right[i]-65].heavy++; > } > } > } > int main(void) > { > Coin coin[12]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}}; > int n,i,count,pos,max; > char left[MAXSIZE*2],right[MAXSIZE*2]; > char result[MAXSIZE]; > scanf("%d",&n); > while(n--) > { > max=0; > memset(coin,0,sizeof(coin)); > for(i=0;i<3;i++) > { > scanf("%s%s%s",left,right,result); > hand(left,right,value(result),coin); > } > for(i=0;i<12;i++) > { > if(!(coin[i].trcoin || (coin[i].heavy>0 && coin[i].light>0))) > { > if(coin[i].light>max) > { > pos=i; > max=coin[i].light; > } > else if(coin[i].heavy>max) > { > pos=i; > max=coin[i].heavy; > } > } > } > if(coin[pos].light>0) printf("%c is the counterfeit coin and it is light.\n",pos+65); > else printf("%c is the counterfeit coin and it is heavy.\n",pos+65); > } > return 0; > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator