厚然在csdn再发一求助帖,把我的积分全拿出来了,说什么这一次程序设计实践也要过了啊,惨啊······

LeeIori 2008-07-05 03:35:19
13. 给定AOE网,输出关键活动。
数据输入:
第一行是一个整数N(1<=N<=20),表示有多少个图需要计算。每个图的第一行是两个整数M,K。M表示AOE网的顶点数,K表示AOE网的边数。顶点默认为1为开始点,M为结束点。从第二行至2+K行为边的信息,每行是三个整数,X,Y,T,表示边从点X到Y,表示此活动的时间为T。
数据输出:
每行输出对应图的关键活动序列,按活动的编号大小升序输出,活动之间用一个空格隔开。
示例:
输入:(标准输入)
2
6 8
1 2 3
1 3 2
2 4 2
2 5 3
3 4 4
3 6 3
4 6 2
5 6 1
4 4
1 2 1
1 3 2
2 4 3
3 4 2
输出:(标准输出)
2 5 7
1 2 3 4

14. 使用快速排序,堆排序(堆顶为最小值),2-归并排序对给出序列进行排序(升序),输出使用以上三种方法排序时关键字比较次数。
数据输入:
第一行是一个整数N(1<=N<=20),表示测试例子数。以后每个测试例子的第一行是一个整数,M,表示整数序列的大小;第二行是整数序列。
数据输出:
每行依次输出对应测试例子的快速排序,堆排序,2-归并排序的关键字比较次数,次数之间用一个空格隔开。
示例:
输入:(标准输入)
2
1 2 3 4 5
2 1 4 3 5
输出:(标准输出)
10 10 8
6 10 8
...全文
179 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangfei_119 2008-07-06
  • 打赏
  • 举报
回复
这个程序是以前写的,有的地方的语法还不是很完美,在TC上可以运行,VC上面需要修改一些地方才可以运行.
建议你对这个程序阅读弄懂,然后在VC中调试通过,那你这个课程设计的目的就达到了.
LeeIori 2008-07-06
  • 打赏
  • 举报
回复
问下一定要在tc上才可以运行吗,vc呢····
LeeIori 2008-07-06
  • 打赏
  • 举报
回复
真是太谢谢你了,小弟感激之情,无以表达,一定好好学习了,争取自己把这程序写出来。
fangfei_119 2008-07-06
  • 打赏
  • 举报
回复
第二部分:

void readcommand(char *cmd){ /*输入指令*/

gotoxy(12,3);
do{*cmd=getchar();}while(*cmd!='1'&&*cmd!='2'&&*cmd!='3'&&*cmd!='q'&&*cmd!='Q');

}


void Interpret(char cmd){ /*处理指令*/
int i,j,n,k , m=0,flag=0,p;
long tt,tm0,tm1;
switch(cmd){
case'1':
flag=1;

for(i=0;i<=groups-1;i++){
if(i<groups/2){
RandomizeList(i,0); /*对正序表作第i级打乱*/
gotoxy(5,i+7);printf(" %d ",m++);putch(0xb3);
}
else{
RandomizeList(groups-i-1,1); /*对逆序表作第i级打乱*/
gotoxy(5,i+7);
if(groups%2==0){printf("-%d ",(m--)-1);putch(0xb3);}
else {printf("-%d ",m--);putch(0xb3);}
}
for(j=0;j<SORTNUM;j++){ /*分别测试排序算法*/
RecallList();
gettime(&t);th0=t.ti_hund;ts0=t.ti_sec;tm0=t.ti_min; /*获取系统时间*/
t0=(tm0*60+ts0)*1000+th0;
for(p=0;p<1000;p++){
(*Sorts[j])(&c,&s);
RecallList();
}
gettime(&t);th1=t.ti_hund;ts1=t.ti_sec;tm1=t.ti_min; /*获取系统时间*/
t1=(tm1*60+ts1)*1000+th1;
tt=t1-t0;/*-tsum;*/
if(choose!='y'){ /*选择显示比较次数和移动次数*/
gotoxy(15+(j-1)*6,7+i);printf("%6ld ",c);
gotoxy(49+(j-1)*6,7+i);printf("%6ld",s);
gotoxy(40,i+7);putch(0xb3);
}
if(choose=='y'){ /*显示排序时间*/
if(flag==0)clrscr();
/**************************************************************************/
gotoxy(1,1);
putch(0xda);
for(p=2;p<79;p++) putch(0xc4); /*print '-'*/
putch(0xbf);
for(p=2;p<25;p++)
{
gotoxy(1,p);putch(0xb3); /*print '|'*/
gotoxy(79,p);putch(0xb3);
}
gotoxy(1,25);
putch(0xc0);
for(p=2;p<79;p++) putch(0xc4);/*print '-'*/
putch(0xd9);
gotoxy(2,2);
printf("********SortTest-1 Size(1---10000)-2 Groups(8--18)-3 Quit-q********");
gotoxy(2,3);
printf(" Command %c Size=%d Groups=%d Time(y/n):%c",cmd,size,groups,choose);
gotoxy(11,3);putch(0x10);
gotoxy(3,4); /*表格起始点*/
putch(0xda);
for(p=2;p<75;p++) putch(0xc4); /*print '-'*/
putch(0xbf);
for(p=5;p<25;p++)
{
gotoxy(3,p);putch(0xb3); /*print '|'*/
gotoxy(77,p);putch(0xb3);
}
gotoxy(3,25);
putch(0xc1);
for(p=2;p<75;p++) putch(0xc4);/*print '-'*/
putch(0xc1);


gotoxy(16,5);
printf(" Time(*1000)/ms ");
gotoxy(9,6);
for(p=0;p<5;p++)
printf(" %s ",SortNames[p]);
gotoxy(27+(j-1)*13,7+i);printf("%ld",tt);

gotoxy(4,5); /*第一列起点*/
printf("Mix ");putch(0xb3);
gotoxy(8,6);putch(0xb3);
/*************************************************************************/
}}
}
break;

case'2':gotoxy(29,3); /*输入测试有序表的长度*/
scanf("%d",&n);size=n;
gotoxy(70,3);
getchar();
choose=getchar();
if(n<1)n=1;
if(n>10000)n=10000;
InitList(n);
break;
case'3': /*输入测试组数*/
gotoxy(52,3);
scanf("%d",&groups);
if(groups<minGroup)groups=minGroup;
if(groups>maxGroup)groups=maxGroup;
break;
}
}


void InitList(int n){ /*构造顺序表*/
int i;
if(n<1)size=0;
else{
if(n>MAXSIZE)n=MAXSIZE;
for(i=1;i<=n;i++)data[i]=data2[i]=i;
size=n;
}
compCount=shiftCount=0;
}

void RandomizeList(int d,int isInverse){ /*对可排序表进行d次随机打乱*/
int a,b,c,i;
if(isInverse)InverseOrder();
else InitList(size);
for(i=1;i<=Mix[d];i++){
a=random(size);
b=random(size);
c=a;a=b;b=c;
c=data[a+1];data[a+1]=data[b+1];data[b+1]=c;
}
for(i=1;i<=size;i++)
data2[i]=data[i];
}

void InverseOrder(){ /*将可排序表置为逆序*/
int i;
for(i=1;i<=size;i++)data[i]=data2[i]=size-i+1;
}


void BeforeSort(){ /*每个排序算法在入口处调用*/
compCount=shiftCount=0;
}

void RecallList(){ /*恢复最后一次用RandomizeList随机打乱后的可排序表*/
int i;
for(i=1;i<=size;i++)
data[i]=data2[i];
}

int Less(int i,int j){ /*比较两个元素大小*/
compCount++;
if(data[i]<data[j])return 1;
else return 0;
}

void Swap(int i,int j){ /*交换两个元素*/
int a;
a=data[i];data[i]=data[j];data[j]=a;
shiftCount=shiftCount+3;
}

void Shift(int i,int j){ /*元素赋值*/
data[j]=data[i];
shiftCount++;
}

void BubbleSort(long *c,long *s){ /*冒泡排序*/
int swapped,i,z;
BeforeSort();
do{
swapped=0;
for(i=1;i<=size-1;i++)
if(Less(i+1,i)){Swap(i+1,i);swapped=1;}
}while(swapped);
*c=compCount;*s=shiftCount;th1-=th0;
}

void InsertSort(long *c,long *s){ /*插入排序*/
int i,j;
BeforeSort();
for(i=2;i<=size;i++){
Shift(i,0);j=i-1;
while(Less(0,j)){Shift(j,j+1);j--;}
Shift(0,j+1);
}
*c=compCount;*s=shiftCount;
}

void SelectSort(long *c,long *s){ /*选择排序*/
int i,j,min;
BeforeSort();
for(i=1;i<=size-1;i++){
min=i;
for(j=i+1;j<=size;j++)
if(Less(j,min))min=j;
if(i!=min)Swap(i,min);
}
*c=compCount;*s=shiftCount;
}

void QuickSort(long *c,long *s){ /*快速排序*/
BeforeSort();
QSort(1,size);
*c=compCount;*s=shiftCount;
}

void QSort(int lo,int hi){
int m,i,j;
if(lo<hi){
i=lo;j=hi;m=(lo+hi)/2;
do{
while(Less(i,m))i++;
while(Less(m,j))j--;
if(i<=j)
if(m==i)m=j;
else if(m==j)m=i;
Swap(i,j);i++;j--;
}while(i<=j);
QSort(lo,j);QSort(i,hi);
}
}

void HeapSort(long *c,long *s){ /*堆排序*/
int left,right;
BeforeSort();
for(left=size/2;left>=1;left--)Sift(left,size);
for(right=size;right>=2;right--)
{Swap(1,right);Sift(1,right-1);}
*c=compCount;*s=shiftCount;
}

void Sift(int left,int right){
int i,j,finished;
i=left;j=2*i;Shift(left,0);finished=0;
Shift(left,MAXSIZE+1);
while(j<=right&&!finished){
if(j<right&&Less(j+1,j))j=j+1;
if(!Less(j,0))finished=1;
else {Shift(j,i);i=j;j=2*i;}
}
Shift(MAXSIZE+1,i);
}
fangfei_119 2008-07-06
  • 打赏
  • 举报
回复
供你参考,自己写的,到TC运行,不过建议自己动手写一下,对自己的能力提高很有帮助.
第一部分:

#define MAXSIZE 10000
#define N 2000
#define SORTNUM 5
#define maxGroup 18
#define minGroup 8
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <dos.h>
typedef int datatype[MAXSIZE+2];
void BubbleSort(long *c,long *s);
void InsertSort(long *c,long *s);
void SelectSort(long *c,long *s);
void QuickSort(long *c,long *s);
void HeapSort(long *c,long *S);
void Sift(int left,int right);
void QSort(int lo,int hi);
void Shift(int i,int j);
void Swap(int i,int j);
int Less(int i,int j);
void RecallList();
void BeforeSort();
void InverseOrder();
void RandomizeList(int d,int isInverse);
void InitList(int n);
void Interpret(char cmd);
void readcommand(char *cmd);
void Initialization();
typedef void (*Func)(long *c,long *s);
static Func Sorts[SORTNUM]={BubbleSort,InsertSort,SelectSort,QuickSort,HeapSort};
static char *SortNames[5]={" Bubble ","Insert ","Selec ","Quick ","Heap"};
static int Mix[]={0,1,4,16,64,128,256,512,4096};

datatype data;
datatype data2;
long compCount,shiftCount;
long c,s,ts0,ts1,th0,th1,t0,t1,t2,t3;
int size,groups;
char choose;
struct time t;
void main(){ /*主函数*/
char cmd;
Initialization(); /*系统初始化,显示界面*/
do{
readcommand(&cmd); /*输入指令*/
Interpret(cmd); /*处理指令*/
}while(cmd!='q'&&cmd!='Q');
}

void Initialization(){ /*初始化函数*/
int i,j;
randomize();
clrscr();
gotoxy(1,1);
putch(0xda);
for(i=2;i<79;i++) putch(0xc4); /*print '-'*/
putch(0xbf);
for(i=2;i<25;i++)
{
gotoxy(1,i);putch(0xb3); /*print '|'*/
gotoxy(79,i);putch(0xb3);
}
gotoxy(1,25);
putch(0xc0);
for(i=2;i<79;i++) putch(0xc4);/*print '-'*/
putch(0xd9);
gotoxy(2,2);
printf("********SortTest-1 Size(1---10000)-2 Groups(8--18)-3 Quit-q********");
gotoxy(2,3);
printf(" Command Size= Groups= Time(y/n):");
gotoxy(11,3);putch(0x10);
gotoxy(3,4); /*表格起始点*/
putch(0xda);
for(i=2;i<75;i++) putch(0xc4); /*print '-'*/
putch(0xbf);
for(i=5;i<25;i++)
{
gotoxy(3,i);putch(0xb3); /*print '|'*/
gotoxy(77,i);putch(0xb3);
}
gotoxy(3,25);
putch(0xc1);
for(i=2;i<75;i++) putch(0xc4);/*print '-'*/
putch(0xc1);


gotoxy(16,5);
printf("compparisonCount shiftCount");
gotoxy(8,6);
for(j=0;j<2;j++){
for(i=0;i<5;i++)
printf("%s",SortNames[i]);
printf(" ");
}
gotoxy(4,5); /*第一列起点*/
printf("Mix ");putch(0xb3);gotoxy(40,5);putch(0xb3);
gotoxy(8,6);putch(0xb3);gotoxy(40,6);putch(0xb3);
}





LeeIori 2008-07-06
  • 打赏
  • 举报
回复
en ,谢了·····
fallening 2008-07-06
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 macfan 的回复:]
上ACM网站找disguss区看去.
[/Quote]
macfan 2008-07-06
  • 打赏
  • 举报
回复
上ACM网站找disguss区看去.
LeeIori 2008-07-05
  • 打赏
  • 举报
回复
`````
angelcm51 2008-07-05
  • 打赏
  • 举报
回复
飘过。。。

今天的任务完成了,睡觉了。
LeeIori 2008-07-05
  • 打赏
  • 举报
回复
是本人水平不怎么样啊,难点的要运用到数据结构的题目都写不出
王旺旺旺 2008-07-05
  • 打赏
  • 举报
回复
有这个上网的时间,何不自己做呢?
小熊大虾 2008-07-05
  • 打赏
  • 举报
回复
路过
lan_6373836 2008-07-05
  • 打赏
  • 举报
回复
楼主
lan_6373836 2008-07-05
  • 打赏
  • 举报
回复
呵呵,看看数据结构书在说话!
baihacker 2008-07-05
  • 打赏
  • 举报
回复
刚看了一下,貌似是ACM的题,而且这个算法也是很中规中矩的.
baihacker 2008-07-05
  • 打赏
  • 举报
回复
路过
ryfdizuo 2008-07-05
  • 打赏
  • 举报
回复
up
LeeIori 2008-07-05
  • 打赏
  • 举报
回复
先谢过各位看帖的大侠了···

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧