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:总是RUNTIME ERROR,那位高手指点一下In Reply To:总是RUNTIME ERROR,那位高手指点一下 Posted by:hj at 2005-10-04 20:07:16 偶的程序也是runtime error,受不了了,本地跑得好好的啊,我也贴出来,高手看看吧! #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXNUM 100000 #define LENGTH 16 typedef struct _resultPair{ char outString[LENGTH]; int countSum; }resultPair; void changeForm(const char *inputString, char *outputString) { unsigned short i, strWidth; char currentChar; char *tempChar=(char *)malloc(2); strWidth=strlen(inputString); for(i=0; i<strWidth; i++) { currentChar=inputString[i]; if(strlen(outputString)==3) strcat(outputString, "-"); if(currentChar>='0'&¤tChar<='9') { sprintf(tempChar, "%c", currentChar); strcat(outputString, tempChar ); } else switch(currentChar) { case 'A': case 'B': case 'C': strcat(outputString, "2"); break; case 'D': case 'E': case 'F': strcat(outputString, "3"); break; case 'G': case 'H': case 'I': strcat(outputString, "4"); break; case 'J': case 'K': case 'L': strcat(outputString, "5"); break; case 'M': case 'N': case 'O': strcat(outputString, "6"); break; case 'P': case 'R': case 'S': strcat(outputString, "7"); break; case 'T': case 'U': case 'V': strcat(outputString, "8"); break; case 'W': case 'X': case 'Y': strcat(outputString, "9"); break; default: strcat(outputString, ""); } } } int main(int argc, char **argv) { unsigned char inputCount; char (*inputString)[MAXNUM]; char (*standardForm)[MAXNUM]; unsigned char *myFlag;//对于每个字符串对应一个flag,置为0表示该串还没有统计;置为1表示该串已经统计 unsigned short i,j,k=0; unsigned short *resultNum; unsigned short *A; resultPair currentMin; char tempChar[LENGTH]; int temp; resultPair *pResultPair; scanf("%d", &inputCount); resultNum=(unsigned short *)malloc(sizeof(unsigned short)*inputCount); myFlag=(unsigned char*)malloc(sizeof(unsigned char)*inputCount); A=(unsigned short *)malloc(sizeof(unsigned short)*inputCount); pResultPair=(resultPair*)malloc(sizeof(resultPair)*(inputCount/2)); inputString = (char (*)[MAXNUM])malloc(MAXNUM*LENGTH) ; standardForm = (char (*)[MAXNUM])malloc(MAXNUM*LENGTH) ; memset(A, -1, sizeof(unsigned short)*inputCount); memset(resultNum, 0, sizeof(unsigned short)*inputCount); memset(myFlag, 0, inputCount); memset(standardForm, 0, MAXNUM*LENGTH); memset(inputString, 0, MAXNUM*LENGTH); for(i=0; i<inputCount; i++) scanf("%s", inputString[i]); for(i=0; i<inputCount; i++) { // printf("the input: %s\n", inputString[i]); changeForm(inputString[i], standardForm[i]); } for(i=0; i<inputCount; i++) for(j=i+1; j<inputCount; j++) { if(myFlag[i]==0) { if(strcmp(standardForm[i], standardForm[j])==0) { resultNum[i]++; myFlag[j]=1; } } } for(i=0; i<inputCount; i++) { if(myFlag[i]==0) continue; else break; } if(i==inputCount) { printf("No duplicates.\n"); free(myFlag); free(resultNum); return 0; } for(i=0; i<inputCount; i++) if(resultNum[i]!=0) { strcpy(pResultPair[k].outString, standardForm[i]); pResultPair[k].countSum=resultNum[i]+1; k++; } for(i=0;i<k;i++)//按字典顺序输出 { strcpy(currentMin.outString, pResultPair[i].outString); currentMin.countSum=pResultPair[i].countSum; for(j=i+1;j<k;j++) if(strcmp(pResultPair[j].outString,currentMin.outString)<0) { strcpy(tempChar, currentMin.outString); strcpy(currentMin.outString,pResultPair[j].outString); strcpy(pResultPair[j].outString, tempChar); temp=currentMin.countSum; currentMin.countSum=pResultPair[j].countSum; pResultPair[j].countSum=temp; } printf("%s %d\n", currentMin.outString, currentMin.countSum); } free(myFlag); free(resultNum); free(A); free(pResultPair); free(inputString); free(standardForm); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator