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 |
Why is my code always wrong? Actually I use dynamic allocation for the array, somebody please help me!!!#include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <set> #include <iostream> using namespace std; const int MAX_LEN=1000; void handle(char * telNumber, int telLen, char * desTel, int desLen) { int desIndex = 0; for(int i=0; i<telLen; i++) { if(telNumber[i]>='0' && telNumber[i]<='9') desTel[desIndex++] = telNumber[i]; else if(telNumber[i]>='A' && telNumber[i]<='C') desTel[desIndex++] = '2'; else if(telNumber[i]>='D' && telNumber[i]<='F') desTel[desIndex++] = '3'; else if(telNumber[i]>='G' && telNumber[i]<='I') desTel[desIndex++] = '4'; else if(telNumber[i]>='J' && telNumber[i]<='L') desTel[desIndex++] = '5'; else if(telNumber[i]>='M' && telNumber[i]<='O') desTel[desIndex++] = '6'; else if( (telNumber[i] == 'P') || (telNumber[i]>='R' && telNumber[i]<='S')) desTel[desIndex++] = '7'; else if(telNumber[i]>='T' && telNumber[i]<='V') desTel[desIndex++] = '8'; else if(telNumber[i]>='W' && telNumber[i]<='Y') desTel[desIndex++] = '9'; else if('-' == telNumber[i]) continue; else desTel[desIndex++] = telNumber[i]; } } void findDupliPrint(char ** desTel, int n) { multiset<string> wordCount; for(int i=0; i<n; i++) wordCount.insert( string(desTel[i]) ); bool noDuplicate = true; for(multiset<string>::iterator it = wordCount.begin(); it != wordCount.end(); ) { string telNumber = *it; int Count = wordCount.count(telNumber), index=0; if(Count > 1) { noDuplicate = false; cout << telNumber.substr(0, 3) << "-" << telNumber.substr(3)<< " " << Count << endl; } while( (index++) < Count ) it++; } if(noDuplicate) cout << "No duplicates. " << endl; } int main() { int n; scanf("%d", &n); // allocate memory for input char ** telNumber = (char **)malloc(sizeof(char)*n*MAX_LEN); for(int i=0; i<n; i++) telNumber[i] = (char *)malloc(sizeof(char)*MAX_LEN); // allocate memory for desination string char ** desTel = (char ** )malloc(sizeof(char)*n*8); for(int i=0; i<n; i++) desTel[i] = (char *)malloc(sizeof(char)*8); for(int i=0; i<n; i++) { scanf("%s", telNumber[i]); handle(telNumber[i], (int)strlen(telNumber[i]), desTel[i], 8); } findDupliPrint(desTel, n); for(int i=0; i<n; i++) free(desTel[i]); free(desTel); for(int i=0; i<n; i++) free(telNumber[i]); free(telNumber); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator