| 5 | 1/1 | 返回列表 |
| 查看: 1250 | 回復(fù): 5 | ||
| 當(dāng)前只顯示滿足指定條件的回帖,點(diǎn)擊這里查看本話題的所有回帖 | ||
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) 其實(shí)按這個程序流程不如構(gòu)造大頂堆效率。。 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 學(xué)碩274求調(diào)劑 +9 | Li李魚 2026-03-26 | 9/450 |
|
|---|---|---|---|---|
|
[考研] 329求調(diào)劑 +6 | 星野? 2026-03-26 | 6/300 |
|
|
[考研] 求調(diào)劑 +6 | 蘆lty 2026-03-25 | 7/350 |
|
|
[考研] 266分,求材料冶金能源化工等調(diào)劑 +7 | 哇呼哼呼哼 2026-03-27 | 9/450 |
|
|
[考研] 283求調(diào)劑 +7 | A child 2026-03-28 | 7/350 |
|
|
[考研] 0703化學(xué)求調(diào)劑,各位老師看看我。。 +5 | 祁祺祺 2026-03-25 | 5/250 |
|
|
[考研] 一志愿上海理工能源動力(085800)310分求調(diào)劑 +3 | zhangmingc 2026-03-27 | 4/200 |
|
|
[考研] 安徽大學(xué)專碩生物與醫(yī)藥專業(yè)(086000)324分,英語已過四六級,六級521,求調(diào)劑 +4 | 美味可樂雞翅 2026-03-26 | 4/200 |
|
|
[考研] 考研調(diào)劑 +9 | 小蠟新筆 2026-03-26 | 9/450 |
|
|
[碩博家園] 北京林業(yè)大學(xué)碩導(dǎo)招生廣告 +6 | kongweilin 2026-03-26 | 8/400 |
|
|
[考研] 一志愿陜師大生物學(xué)071000,298分,求調(diào)劑 +5 | SYA! 2026-03-23 | 5/250 |
|
|
[考研]
材料學(xué)碩,求調(diào)劑
6+5
|
糖葫蘆888ll 2026-03-22 | 10/500 |
|
|
[考研] 求調(diào)劑 一志愿 本科 北科大 化學(xué) 343 +6 | 13831862839 2026-03-24 | 7/350 |
|
|
[考研] 321求調(diào)劑 +6 | wasdssaa 2026-03-26 | 6/300 |
|
|
[考研] 327求調(diào)劑 +7 | prayer13 2026-03-23 | 7/350 |
|
|
[考研] 297求調(diào)劑 +6 | 田洪有 2026-03-26 | 6/300 |
|
|
[考研] 一志愿武理085500機(jī)械專業(yè)總分300求調(diào)劑 +3 | an10101 2026-03-24 | 7/350 |
|
|
[考研] 一志愿吉大化學(xué)322求調(diào)劑 +4 | 17501029541 2026-03-23 | 6/300 |
|
|
[考研] 一志愿國科過程所081700,274求調(diào)劑 +3 | 三水研0水立方 2026-03-23 | 3/150 |
|
|
[考研] 275求調(diào)劑 +6 | shansx 2026-03-22 | 8/400 |
|