写代码写的是真不熟练了,或者说是从来就没熟练过。第一遍写的代码就不再这儿贴了,那代码写的真是思路混乱,代码混乱不清晰。
由于是第一次,写完提交后显示为Wrong answer,看了别人写的好代码就直接修改了,没有把原始的版本保留下来,下次一定保留下来,和更优秀的代码对比,找到差距。
第一遍写完后,看了别人的代码后有一个感觉很强烈:读完题后一定要把思路理清楚,把各个模块的边界理顺或者说把模块图画出来后再开始coding。
以下贴一个别人的代码,在POJ上他的名字叫 CLANNAD_WAWA:
#include "stdio.h"void Max_Heapify (int source[], int i, int size)
{int left = i * 2 + 1;int right = i * 2 + 2;int largest;if (left <= size - 1 && source[left] > source[i]){largest = left;}else{largest = i;}if (right <= size - 1 && source[right] > source[largest]){largest = right;}if (largest != i){int temp;temp = source[i];source[i] = source[largest];source[largest] = temp;Max_Heapify (source, largest, size);}
}
void Max_Heap (int source[], int size)
{int i;for (i = (size - 1) / 2; i >= 0; --i){Max_Heapify (source, i, size);}
}void HeapSort (int source[], int size)
{int temp;int i;Max_Heap (source, size);for (i = size - 1; i >= 1; --i){temp = source[i];source[i] = source[0];source[0] = temp;--size;Max_Heapify (source, 0, size);}
}void main()
{int n; char TelTemp[50];int TelTable[100000];int i;int j;char symbol_table[100];int Frequency = 1; int have_result = 0;symbol_table['0'] = 0;symbol_table['1'] = 1;symbol_table['A'] = symbol_table['B'] = symbol_table['C'] = symbol_table['2'] = 2;symbol_table['D'] = symbol_table['E'] = symbol_table['F'] = symbol_table['3'] = 3;symbol_table['G'] = symbol_table['H'] = symbol_table['I'] = symbol_table['4'] = 4;symbol_table['J'] = symbol_table['K'] = symbol_table['L'] = symbol_table['5'] = 5;symbol_table['M'] = symbol_table['N'] = symbol_table['O'] = symbol_table['6'] = 6;symbol_table['P'] = symbol_table['R'] = symbol_table['S'] = symbol_table['7'] = 7;symbol_table['T'] = symbol_table['U'] = symbol_table['V'] = symbol_table['8'] = 8;symbol_table['W'] = symbol_table['X'] = symbol_table['Y'] = symbol_table['9'] = 9;scanf ("%d", &n);//getchar();for (i = 0; i < n; ++i){ int times = 6; TelTable[i] = 0;scanf ("%s", TelTemp);for (j = 0; j < 50; ++j){if ((TelTemp[j] >= 'A' && TelTemp[j] <= 'Z') || (TelTemp[j] >= '0' && TelTemp[j] <= '9')){TelTable[i] = TelTable[i] * 10 + symbol_table[TelTemp[j]];--times;if (times < 0){break;}} }}HeapSort (TelTable, n);for (i = 0; i < n - 1; ++i){if (TelTable[i] == TelTable[i + 1]){++Frequency;}else{if (Frequency >= 2){printf ("%03ld-%04ld %d\n", TelTable[i] / 10000, TelTable[i] - TelTable[i] /10000 * 10000, Frequency);Frequency = 1;have_result = 1;}}}if (Frequency >= 2){printf ("%03ld-%04ld %d\n", TelTable[i] / 10000, TelTable[i] - TelTable[i] /10000 * 10000, Frequency);Frequency = 1;have_result = 1;}if (have_result == 0){printf ("No duplicates.");}
}
此作者的思路很清晰,代码模块很明确,在看了作者的思路后,我仿写了如下代码。
#include"stdio.h"
#define LENGTH 50void adjust_heap(int *input, int root, int size)
{int swap=0;int left=2*root+1;int right=2*root+2;int largest=root;while(root<size){if((left<size) || (right<size)){if((left<size) && (input[largest]<input[left])){largest=left;}if((right<size) && (input[largest]<input[right])){largest=right;}}if(largest!=root){swap=input[largest];input[largest]=input[root];input[root]=swap;left=2*largest+1;right=2*largest+2;root=largest; }else{break;}}
}
void build_maxheap(int *input,int size)
{int i=0;int lastend_root=size/2-1;for(i=lastend_root;i>=0;i--){adjust_heap(input,i,size);}
}void heap_sort(int *input,int size)
{int swap=0;build_maxheap(input,size);while(size>1){swap=input[0];input[0]=input[size-1];input[size-1]=swap;size--;adjust_heap(input,0,size);}
}void main()
{int n=0;int temp=0;int count=0;char printed=0;int i=0,j=0;char input[LENGTH];int mid_output[100000]={0};int map_table[100];map_table['0']=0;map_table['1']=1;map_table['2']=map_table['A']=map_table['B']=map_table['C']=2;map_table['3']=map_table['D']=map_table['E']=map_table['F']=3;map_table['4']=map_table['G']=map_table['H']=map_table['I']=4;map_table['5']=map_table['J']=map_table['K']=map_table['L']=5;map_table['6']=map_table['M']=map_table['N']=map_table['O']=6;map_table['7']=map_table['P']=map_table['R']=map_table['S']=7;map_table['8']=map_table['T']=map_table['U']=map_table['V']=8;map_table['9']=map_table['W']=map_table['X']=map_table['Y']=9;///步骤一:将字符串转化为数字scanf("%d",&n);for(i=0; i<n; i++){scanf("%s",input);j=0;while(input[j]){if(((input[j]>='0') && (input[j]<='9')) || ((input[j]>='A') && (input[j]<='Z'))){mid_output[i]=10*mid_output[i]+map_table[input[j]];j++;}else{j++;}}}///步骤二:将数字排序heap_sort(mid_output,n);///步骤三:输出结果for(i=0;i<n;){temp=mid_output[i];while((temp==mid_output[i]) && (i<n)){count++;i++;}if(count>=2){printf("%d-%d %d\n",temp/10000,temp%10000,count);printed=1;count=0;}else{count=0;} }if(0==printed){printf("No duplicates.");}
}
思路和CLANNAD_WAWA的思路一样,但是由于没有细看CLANNAD_WAWA的代码,实现细节还是不完全一样。
遗留问题:
但是令我百思不得其解的事儿是,我的代码提交后还是现实Wrong Answer,不知道是后台的那个测试用例跑不过。
后来,我就替换了和CLANNAD_WAWA对应模块的代码,发现是步骤三出了问题,首先我得承认CLANNAD_WAWA,在步骤三种的写法确实比我的写法好,但是我实在是找不出我是为什么错的,如果那位高手看到,还望多多指教。
??