| 6 | 1/1 | 返回列表 |
| 查看: 1248 | 回復(fù): 5 | ||
xiaolei@金蟲 (小有名氣)
|
[求助]
求大俠幫忙看個程序
|
|
#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ù)組,長度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; } } 誰能幫忙看下這個程序的每個函數(shù)的時間復(fù)雜度空間復(fù)雜度是多少?還有最后個希爾排序算法每步的意思。謝謝啦 |

金蟲 (正式寫手)
Holy Shit

金蟲 (正式寫手)
Holy Shit

金蟲 (小有名氣)

新蟲 (初入文壇)
|
這個程序就是輸入侯選人票數(shù)并做個希爾排序么。。 關(guān)于排序算法建議回去看數(shù)據(jù)結(jié)構(gòu)。。。 希爾排序的時間復(fù)雜度是:O(nlogn)~O(n2),平均時間復(fù)雜度大致是O(n^1.5) 其實按這個程序流程不如構(gòu)造大頂堆效率。。 |
| 6 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 275求調(diào)劑 +10 | Micky11223 2026-03-25 | 14/700 |
|
|---|---|---|---|---|
|
[考研] 一志愿北化085600材料專碩275|有文章專利|求調(diào)劑 +5 | Micky11223 2026-03-25 | 5/250 |
|
|
[考研] 調(diào)劑求院校招收 +4 | 鶴鯨鴿 2026-03-28 | 4/200 |
|
|
[考研] 291求調(diào)劑 +15 | hhhhxn.. 2026-03-23 | 21/1050 |
|
|
[考研] 材料求調(diào)劑一志愿哈工大324 +7 | 閆旭東 2026-03-28 | 9/450 |
|
|
[考研] 0703化學(xué)/290求調(diào)劑/本科經(jīng)歷豐富/工科也可 +9 | 丹青奶蓋 2026-03-26 | 10/500 |
|
|
[考研] 322求調(diào)劑 +4 | 我真的很想學(xué)習(xí) 2026-03-23 | 4/200 |
|
|
[考研] 085601 材料工程 313分 求調(diào)劑 +5 | Ong3 2026-03-27 | 5/250 |
|
|
[考研] 333求調(diào)劑 +3 | question挽風(fēng) 2026-03-23 | 3/150 |
|
|
[考研] 求調(diào)劑 +3 | 劉柯@ 2026-03-24 | 4/200 |
|
|
[考研] 316求調(diào)劑 +5 | 江辭666 2026-03-26 | 5/250 |
|
|
[考研] 336材料求調(diào)劑 +7 | 陳瀅瑩 2026-03-26 | 9/450 |
|
|
[考研] 一志愿北化求調(diào)劑 +3 | Jsman 2026-03-22 | 3/150 |
|
|
[考研] 321求調(diào)劑 +6 | Ymlll 2026-03-24 | 6/300 |
|
|
[考研] 一志愿河工大 081700 276求調(diào)劑 +4 | 地球繞著太陽轉(zhuǎn) 2026-03-23 | 4/200 |
|
|
[考研] 一志愿天津大學(xué)339材料與化工求調(diào)劑 +3 | 江往賣魚 2026-03-26 | 3/150 |
|
|
[考研] 0854電子信息求調(diào)劑 +7 | α____ 2026-03-22 | 9/450 |
|
|
[考研] 0854電子信息求調(diào)劑 324 +4 | Promise-jyl 2026-03-23 | 4/200 |
|
|
[考研] 材料專碩找調(diào)劑 +5 | 哈哈哈吼吼吼哈 2026-03-23 | 5/250 |
|
|
[考研] 一志愿北京化工大學(xué)070300 學(xué)碩336求調(diào)劑 +5 | vv迷 2026-03-21 | 8/400 |
|