| 6 | 1/1 | 返回列表 |
| 查看: 1247 | 回復(fù): 5 | ||
xiaolei@金蟲 (小有名氣)
|
[求助]
求大俠幫忙看個(gè)程序
|
|
#include #include typedef struct node { char name[8]; int num; int score; int tax; }Node; // 候選人結(jié)構(gòu) void shellSort( Node **, int ); int main(void) { int n = 0; Node * pArray[9];//指針數(shù)組,長(zhǎng)度9 int count = 0; //int status = 1; int vote = -1; printf("Input the number of the candidates(1-9):\n" ;scanf("%d", &n); while(getchar()!='\n') { ; } while (n>9 || n<1) { if (n>9) { printf("No, there cannot be so many candidates. Retry.\n" ;} else { printf("No candidates? It cannot be! Retry!\n" ;} scanf("%d", &n); while(getchar()!='\n') { ; } } for (count=0; count pArray[count] = (Node *)malloc(sizeof(Node)); pArray[count]->num = count+1; pArray[count]->tax = 0; pArray[count]->score = 0; printf("Input No.%d candidate's name:\n", count+1); gets(pArray[count]->name); } while (vote) { printf("Now, let us vote:\n*************\n" ;for (count=0; count printf("%d. %s\n", count+1, pArray[count]->name); } printf("0.quit\n*************\n" ;scanf("%d", &vote); while(getchar()!='\n') { ; } while (vote<0 || vote>n) { printf("No joke, thank you. Revote.\n" ;scanf("%d", &vote); while(getchar()!='\n') { ; } } if (vote>0&&vote<=n) { pArray[vote-1]->score++; } } printf("Finish voting. Let's find the winner......\n\n" ;shellSort( pArray, n ); for (count=0; count pArray[count]->tax=count+1; printf("%d. %s %d votes.\n", count+1, pArray[count]->name, pArray[count]->score); } for(count=0; count free(pArray[count]); pArray[count] = NULL; } return 0; } void shellSort( Node *p[], int len ) { const int Length = len; int i = 0; int j = 0; int gap = 0; Node *temp = NULL; gap = Length/2; while (gap>0) { for (i=gap; i j = i - gap; temp = *(p+i); while ( (j>=0) && (p[j]->score < temp->score) ) { *(p+j+gap) = *(p+j); j = j - gap; } *(p+j+gap) = temp; } gap /= 2; } } 誰(shuí)能幫忙看下這個(gè)程序的每個(gè)函數(shù)的時(shí)間復(fù)雜度空間復(fù)雜度是多少?還有最后個(gè)希爾排序算法每步的意思。謝謝啦 |

金蟲 (正式寫手)
Holy Shit

金蟲 (正式寫手)
Holy Shit

金蟲 (小有名氣)

新蟲 (初入文壇)
|
這個(gè)程序就是輸入侯選人票數(shù)并做個(gè)希爾排序么。。 關(guān)于排序算法建議回去看數(shù)據(jù)結(jié)構(gòu)。。。 希爾排序的時(shí)間復(fù)雜度是:O(nlogn)~O(n2),平均時(shí)間復(fù)雜度大致是O(n^1.5) 其實(shí)按這個(gè)程序流程不如構(gòu)造大頂堆效率。。 |
| 6 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|