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 |
出人命了!测试数据都对了可wrong了N次:大侠几个让我wrong的数据或建议吧,不胜感激://优化 #include <stdio.h> #include <string.h> #define OK 1 #define ERROR 0 #define HALF_STRING 4 #define LEFT -1 #define RIGHT 1 #define FALSE 0 typedef int status; typedef struct { char left[13]; char right[13]; } line; status initLineArray(line string[3],int *countEven); status solution(line string[3],int countEven); int locate(line *string,char counterfeit); void printfResult(char weight[],int state,char counterfeit); main() { int n,countEven; line string[3]; fp = fopen("data.txt","r"); scanf("%d",&n); while(n--) { countEven = 0; initLineArray(string,&countEven); solution(string,countEven); } } status initLineArray(line string[3],int *countEven) { char left[5],right[5],weigh[5]; int first = 0 ,last = 2,i; for( i = 0;i < 3 ;i++) { scanf("%s %s %s",left,right,weigh); if(!strcmp(weigh,"even")) { (*countEven)++; strcpy(string[last].left,left); strcpy(string[last].right,right); last--; } else if(!strcmp(weigh,"down")) { strcpy(string[first].left,right); strcpy(string[first].right,left); first++; } else { strcpy(string[first].left,left); strcpy(string[first].right ,right); first++; } } return OK; } status solution(line string[3],int countEven) { int i,j,state[3]; char counterfeit; char weight [6]; for(i = 'A';i <= 'L';i++) { counterfeit = i; for(j = 0;j < 3;j++) state[j] = locate(&string[j],counterfeit); switch (countEven) { case 0: if(state[0] == state[1] && state[2] == state[1] && state[0]) i = 'L'; break; case 1: if(state[0] == state[1] && state[0] && !state[2]) i = 'L'; break; case 2: if(state[0] && !state[1] && !state[2]) i = 'L'; break; default: break; } } printfResult(weight,state[0],counterfeit); return OK; } int locate(line *string,char counterfeit) { int i; for(i = 0;i < sizeof(string->left);i++) { if(*(string->left + i) == counterfeit) return LEFT; } for(i = 0;i < sizeof(string->right);i++) { if(*(string->right + i) == counterfeit) return RIGHT; } return FALSE; } void printfResult(char weight[],int state,char counterfeit) { if(state == -1) { strcpy(weight,"heavy"); } else if(state == 1) { strcpy(weight,"light"); } printf("%c is the counterfeit coin and it is %s.\n",counterfeit,weight); } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator