24小时热门版块排行榜    

查看: 1386  |  回复: 5
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

xiaolei@

金虫 (小有名气)

[求助] 求大侠帮忙看个程序

#include
#include

typedef struct node
{   char name[8];
int num;
int score;
int tax;
}Node;     //  候选人结构


void shellSort( Node **, int );

int main(void)
{
int n = 0;
Node * pArray[9];//指针数组,长度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;
}
}
谁能帮忙看下这个程序的每个函数的时间复杂度空间复杂度是多少?还有最后个希尔排序算法每步的意思。谢谢啦
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

英雄就是做任何事都全力以赴!!!
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

xiaolei@

金虫 (小有名气)

引用回帖:
: Originally posted by lt292 at 2011-12-30 16:20:54:
lz程序建议给个缩进啊,看起来层次有点乱
编程风格很重要

额....还是初学...不太会
英雄就是做任何事都全力以赴!!!
4楼2011-12-30 17:12:08
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 6 个回答

lt292

金虫 (正式写手)

Holy Shit

我表示,我已经看不懂指针是什么意思了
好久没看到c程序了
记忆中的颜色,慢慢褪去,变成一缕缕的黑丝,最终消失在茫茫的空白里,一去不返```
2楼2011-12-30 16:19:25
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lt292

金虫 (正式写手)

Holy Shit

xzhdty: 欢迎常来 2011-12-30 20:09:50
lz程序建议给个缩进啊,看起来层次有点乱
编程风格很重要
记忆中的颜色,慢慢褪去,变成一缕缕的黑丝,最终消失在茫茫的空白里,一去不返```
3楼2011-12-30 16:20:54
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

jxt1234

新虫 (初入文坛)

【答案】应助回帖

★ ★
感谢参与,应助指数 +1
余泽成(金币+2): 谢谢参与应助,新年快乐! 2011-12-31 18:27:09
xiaolei@(金币+10): ★★★很有帮助 谢谢啦 课程设计要求 所以才这样的。 2011-12-31 19:42:43
这个程序就是输入侯选人票数并做个希尔排序么。。
关于排序算法建议回去看数据结构。。。
希尔排序的时间复杂度是:O(nlogn)~O(n2),平均时间复杂度大致是O(n^1.5)
其实按这个程序流程不如构造大顶堆效率。。
5楼2011-12-31 15:58:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 重要来源:本周末出结果 +5 瞬息宇宙 2026-08-12 5/250 2026-08-13 07:13 by athenadai219
[基金申请] 应该是93bebmhtak前后十一个字符比较关键 +22 Lanmanbaby 2026-08-09 36/1800 2026-08-12 22:50 by sdfapple719
[基金申请] 长年满屏的广告,版主太不责任了。基金也等的急 +4 gltch 2026-08-12 4/200 2026-08-12 16:08 by eulota
[基金申请] filecode +13 documentary 2026-08-10 15/750 2026-08-12 15:56 by 云上清扬
[基金申请] 奇怪,两个人的filecode固定段从头到尾一模一样 +7 布布和一二 2026-08-10 10/500 2026-08-12 09:12 by 布布和一二
[基金申请] 小木虫上这么多卖论文的,真有人买论文么?感觉没必要啊 +8 Tide man 2026-08-10 9/450 2026-08-11 20:39 by beefly
[硕博家园] 读博的好处 +3 lnee 2026-08-11 3/150 2026-08-11 18:10 by 希望我好好的
[基金申请] 分享一下我之前已中青C的计划书的filecode +3 布布和一二 2026-08-11 4/200 2026-08-11 15:20 by aasahr
[基金申请] 我的国基提前知道中了,可是同事的操作让我实在接受不了,怎么会有这样的人 +8 家与远方 2026-08-10 13/650 2026-08-11 09:49 by 家与远方
[基金申请] 基金中了 +15 laoda193707 2026-08-06 15/750 2026-08-11 00:11 by jiafei2190
[基金申请] 据悉今年马上要出结果了 +7 瞬息宇宙 2026-08-10 8/400 2026-08-10 12:42 by Vivilian
[基金申请] 2026国自然放榜时间 +9 布布和一二 2026-08-08 9/450 2026-08-10 11:22 by xxxx2020
[基金申请] 这样的filecode谁见过 +11 布布和一二 2026-08-08 22/1100 2026-08-10 11:10 by wmfsnow
[基金申请] filecode与中标关系的预测 +5 布布和一二 2026-08-07 5/250 2026-08-09 16:15 by 袁向阳007
[基金申请] fileCode有新解读? +10 Tide man 2026-08-08 18/900 2026-08-09 12:55 by 仁砚薪传
[基金申请] 关于filecode,很负责任的告诉大家 +6 爱看书的可乐 2026-08-08 7/350 2026-08-08 22:13 by a_niu
[基金申请] 国基金的申报应该改成非等额制,评价高的钱多评价低的钱少,但是增加资助率 +7 a089 2026-08-07 7/350 2026-08-08 18:05 by gltch
[基金申请] 化学口download_prp&amp;fileCode的固定段好像这几天一直没变,有变的大神么? +3 Tide man 2026-08-07 4/200 2026-08-07 22:39 by Tide man
[基金申请] filecode变化情况 +6 布布和一二 2026-08-07 22/1100 2026-08-07 14:45 by 且听虎啸
[基金申请] 大家散了吧,后缀研究没有意义,别浪费时间了,过好目前的每一天,不要焦虑 +5 Tide man 2026-08-06 7/350 2026-08-07 13:11 by 医学老男孩
信息提示
请填处理意见