日历
统计
- 随笔 - 30
- 文章 - 0
- 评论 - 51
- 引用 - 0
导航
常用链接
留言簿(4)
随笔分类
随笔档案
ACM
搜索
最新评论
阅读排行榜
评论排行榜
|
今天下午做了一道acm的题,提交了10次都是WA,所以想请大家帮我看看到底哪里不正确,程序哪里写的不好!谢谢大家了! 代码:
1#include <stdlib.h> 2#include <stdio.h> 3typedef struct telNumber 4{ 5 int fNumber; 6 int lNumber; 7 int count; 8} TelNumber; 9 10int toNumber[26] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,-1,7,7,8,8,8,9,9,9,-1}; 11 12int ConvertToNumber(char *ch, TelNumber *temp) 13{ 14 int tmp; 15 tmp = 0; 16 17 if( !ToNumber(ch, &tmp, 7)) 18 19 return 0; 20 21 temp->fNumber = tmp/10000; 22 temp->lNumber = tmp%10000; 23 temp->count = 1; 24 25 return 1; 26 27} 28 29int ToNumber(char *c, int *number,int count) 30{ 31 32 int tmp = 0; 33 int i; 34 for( i = 0; i < count; ) 35 { 36 37 if( *c == '-') 38 39 { 40 c++; 41 continue; 42 } 43 44 else if( *c >= '0' && *c <= '9') 45 { 46 tmp = tmp*10 + (*c) - '0'; 47 } 48 else if(*c >= 'A' && *c <= 'Z') 49 { 50 if(toNumber[*c-'A'] > 0) 51 { 52 tmp = tmp * 10 + toNumber[*c-'A']; 53 } 54 else 55 return 0; 56 } 57 else 58 return 0; 59 i++; 60 c++; 61 62 } 63 *number = tmp; 64 return 1; 65 66} 67void SortNumber(TelNumber **tel, int left, int right) 68{ 69 int p; 70 71 if(left >= right) 72 return; 73 74 p = Partition(tel,left, right); 75 SortNumber(tel, left, p-1); 76 SortNumber(tel, p+1, right); 77 78} 79int Partition(TelNumber **tel, int left, int right) 80{ 81 int j = left + 1; 82 int i; 83 84 TelNumber *temp = tel[(left+right)/2]; 85 tel[(left+right)/2] = tel[left]; 86 tel[left] = temp; 87 88 89 for( i = j; i <= right; i++) 90 { 91 if(tel[i]->fNumber < tel[left]->fNumber || (tel[i]->fNumber == tel[left]->fNumber && tel[i]->lNumber <= tel[left]->lNumber)) 92 { 93 temp = tel[i]; 94 tel[i] = tel[j]; 95 tel[j] = temp; 96 j++; 97 } 98 } 99 100 temp = tel[j-1]; 101 tel[j-1] = tel[left]; 102 tel[left ] = temp; 103 return j -1; 104} 105int Combinate(TelNumber **tel, int count) 106{ 107 int t = 0; 108 int i; 109 for( i = 0; i < count;) 110 { 111 int j = i+1; 112 while(j < count) 113 { 114 if(tel[i]->fNumber == tel[j]->fNumber && tel[i]->lNumber == tel[j]->lNumber) 115 { 116 j++; 117 } 118 else 119 break; 120 } 121 122 tel[i]->count = j-i; 123 tel[t] = tel[i]; 124 t++; 125 i = j; 126 } 127 return t; 128} 129 130int main(int argc, char* argv[]) 131{ 132 133 int count; 134 int p = 0; 135 int tc = 0; 136 int i; 137 int t; 138 TelNumber **tel; 139 scanf("%d", &count); 140 tel = (TelNumber**) malloc(sizeof(TelNumber*)*count); 141 for( i = 0; i < count; i++) 142 { 143 char ch[100]; 144 TelNumber *temp = (TelNumber*)malloc(sizeof(TelNumber)); 145 temp->fNumber = 0; 146 temp->count = 0; 147 temp->lNumber = 0; 148 scanf("%s", ch); 149 if(!ConvertToNumber(ch, temp)) 150 continue; 151 tel[tc++] = temp; 152 } 153 154 SortNumber(tel, 0, tc-1); 155 t = Combinate(tel, tc); 156 if(t == tc) 157 { 158 printf("No duplicates.\n"); 159 } 160 else 161 { 162 for( i = 0; i < t; i++) 163 { 164 if(tel[i]->count > 1) 165 { 166 printf("%d-%d %d\n",tel[i]->fNumber,tel[i]->lNumber,tel[i]->count); 167 } 168 } 169 } 170 return 0; 171} 172 173
|