程序设计课.docx
- 文档编号:10306205
- 上传时间:2023-02-10
- 格式:DOCX
- 页数:44
- 大小:25.40KB
程序设计课.docx
《程序设计课.docx》由会员分享,可在线阅读,更多相关《程序设计课.docx(44页珍藏版)》请在冰豆网上搜索。
程序设计课
汉诺塔:
#include
inthj(inta,intb,intc,inti)
{
intt;
if(i==1)
printf("%d->%d\n",a,c);
else
{t=c;
c=b;
b=t;
hj(a,b,c,i-1);
printf("%d->%d\n",a,b);
t=a;
a=c;
c=t;
t=b;
b=c;
c=t;
hj(a,b,c,i-1);
return0;
}
}
main()
{
inta,b,c,i;
a=1;
b=2;
c=3;
printf("
请输入汉诺塔的盘数
");
scanf("%d",&i);
hj(a,b,c,i);
return0;
}
快速排序:
#include
#include
#defineN6
intpartition(intarr[],intlow,inthigh){
intkey;
key=arr[low];
while(low while(low high--; if(low arr[low++]=arr[high]; while(low low++; if(low arr[high--]=arr[low]; } arr[low]=key; returnlow; } voidquick_sort(intarr[],intstart,intend){ intpos; if(start pos=partition(arr,start,end); quick_sort(arr,start,pos-1); quick_sort(arr,pos+1,end); } return; } intmain(void){ inti; intarr[N]={32,12,7,78,23,45}; printf("排序前\n"); for(i=0;i printf("%d\t",arr[i]); quick_sort(arr,0,N-1); printf("\n排序后\n"); for(i=0;i printf("%d\t",arr[i]); printf("\n"); system("pause"); return0; } 最长公共子序列: #include #include #defineN20 voidLCSLength(intm,intn,charx[N+1],chary[N+1],charb[N+1][N+1]); voidLCS(inti,intj,charx[N+1],charb[N+1][N+1]); voidmain() { intlx,ly; charX[N+1],Y[N+1]; charB[N+1][N+1]; printf("请输入X序列: \n"); scanf("%s",X+1); printf("请输入Y序列: \n"); scanf("%s",Y+1); lx=strlen(X+1); printf("X序列的长度: "); printf("%d\n",lx); ly=strlen(Y+1); printf("Y序列的长度: "); printf("%d\n",ly); LCSLength(lx,ly,X,Y,B); LCS(lx,ly,X,B); } voidLCSLength(intm,intn,charx[],chary[],charb[N+1][N+1]) { inti,j,s,t; intc[N+1][N+1]; for(i=0;i<=m;i++) c[i][0]=0; for(i=0;i<=n;i++) c[0][i]=0; for(i=1;i<=m;i++) for(j=1;j<=n;j++) { if(x[i]==y[j]) { c[i][j]=c[i-1][j-1]+1; b[i][j]='A'; } elseif(c[i-1][j]>=c[i][j-1]) { c[i][j]=c[i-1][j]; b[i][j]='U'; } else { c[i][j]=c[i][j-1]; b[i][j]='L'; } } for(s=0;s<=m;s++) { for(t=0;t<=n;t++) printf("%d",c[s][t]); printf("\n"); } printf("length=%d\n",c[m][n]); } voidLCS(inti,intj,charx[],charb[N+1][N+1]) { if(i==0||j==0) return; if(b[i][j]=='A') { LCS(i-1,j-1,x,b); printf("%c",x[i]); } elseif(b[i][j]=='U') LCS(i-1,j,x,b); else LCS(i,j-1,x,b); } 哈夫曼编码: #include #include #include #include #defineM100 typedefstructFano_Node { charch; floatweight; }FanoNode[M]; typedefstructnode { intstart; intend; structnode*next; }LinkQueueNode; typedefstruct { LinkQueueNode*front; LinkQueueNode*rear; }LinkQueue; //建立队列 voidEnterQueue(LinkQueue*q,ints,inte) { LinkQueueNode*NewNode; //生成新节点 NewNode=(LinkQueueNode*)malloc(sizeof(LinkQueueNode)); if(NewNode! =NULL) { NewNode->start=s; NewNode->end=e; NewNode->next=NULL; q->rear->next=NewNode; q->rear=NewNode; } else { printf("Error! "); exit(-1); } } //按权分组 voidDivide(FanoNodef,ints,int*m,inte) { inti; floatsum,sum1; sum=0; for(i=s;i<=e;i++) sum+=f[i].weight;// *m=s; sum1=0; for(i=s;i { sum1+=f[i].weight; *m=fabs(sum-2*sum1)>fabs(sum-2*sum1-2*f[i+1].weight)? (i+1): *m; if(*m==i)break; } } voidmain() { inti,j,n,max,m,h[M]; intsta,end; floatw; charc,fc[M][M]; FanoNodeFN; LinkQueueNode*p; LinkQueue*Q; //初始化队Q Q=(LinkQueue*)malloc(sizeof(LinkQueue)); Q->front=(LinkQueueNode*)malloc(sizeof(LinkQueueNode)); Q->rear=Q->front; Q->front->next=NULL; printf("\t***FanoCoding***\n"); printf("Pleaseinputthenumberofnode: "); //输入信息 scanf("%d",&n); //超过定义M,退出 if(n>=M) { printf(">=%d",M); exit(-1); } i=1;//从第二个元素开始录入 while(i<=n) { printf("%dweightandnode: ",i); scanf("%f%c",&FN[i].weight,&FN[i].ch); for(j=1;j { if(FN[i].ch==FN[j].ch)//查找重复 { printf("Samenode! ! ! \n");break; } } if(i==j) i++; } //排序(降序) for(i=1;i<=n;i++) { max=i+1; for(j=max;j<=n;j++) max=FN[max].weight j: max; if(FN[i].weight { w=FN[i].weight; FN[i].weight=FN[max].weight; FN[max].weight=w; c=FN[i].ch; FN[i].ch=FN[max].ch; FN[max].ch=c; } } for(i=1;i<=n;i++)//初始化h h[i]=0; EnterQueue(Q,1,n);//1和n进队 while(Q->front->next! =NULL) { p=Q->front->next;//出队 Q->front->next=p->next; if(p==Q->rear) Q->rear=Q->front; sta=p->start; end=p->end; free(p); Divide(FN,sta,&m,end);/*按权分组*/ for(i=sta;i<=m;i++) { fc[i][h[i]]='0'; ++h[i]; } if(sta! =m) EnterQueue(Q,sta,m); else fc[sta][h[sta]]='\0'; for(i=m+1;i<=end;i++) { fc[i][h[i]]='1'; ++h[i]; } if(m==sta&&(m+1)==end) //如果分组后首元素的下标与中间元素的相等, //并且和最后元素的下标相差为1,则编码码字字符串结束 { fc[m][h[m]]='\0'; fc[end][h[end]]='\0'; } else EnterQueue(Q,m+1,end); } for(i=1;i<=n;i++)/*打印编码信息*/ { printf("%c: ",FN[i].ch); printf("%s\n",fc[i]); } system("pause"); } 最短路径: #include #include usingnamespacestd; voidDijkstra(intn,intv,intdist[],intprev[],int**c) { intmaxint=65535; bool*s=newbool[n]; for(inti=1;i<=n;i++) { dist[i]=c[v][i]; s[i]=false; if(dist[i]==maxint) { prev[i]=0; } else { prev[i]=v; } } dist[v]=0; s[v]=true; for(inti=1;i { inttemp=maxint; intu=v; for(intj=1;j<=n;j++) { if((! s[j])&&(dist[j] { u=j; temp=dist[j]; } } s[u]=true; for(intj=1;j<=n;j++) { if((! s[j])&&(c[u][j] { intnewdist=dist[u]+c[u][j]; if(newdist { dist[j]=newdist; prev[j]=u; } } } } } intmain() { intn,v,u; intq=0; cout<<"输入顶点数: "; cin>>n; int*way=newint[n+1]; int**c=newint*[n+1]; for(inti=1;i<=n;i++) { c[i]=newint[n+1]; } cout<<"输入顶点的邻接矩阵: "; for(intj=1;j<=n;j++) { for(intt=1;t<=n;t++) { cin>>c[j][t]; } } int*dist=newint[n]; int*prev=newint[n]; cout<<"输入出发点和到达点: "; cin>>v>>u; Dijkstra(n,v,dist,prev,c); cout<<"最短路径从"< "< intw=u; while(w! =v) { q++; way[q]=prev[w]; w=prev[w]; } cout<<"路径为: "; for(intj=q;j>=1;j--) { cout< } cout< delete[]way;way=NULL; for(inti=1;i<=n;i++)delete[]c[i]; delete[]c; c=NULL; delete[]dist;dist=NULL; delete[]prev;prev=NULL; system("pause"); return0; } 马踏棋盘: #defineSTACK_INIT_SIZE100 #defineSTACKINCREMENT10 #defineOVERFLOW-2 #defineOK1 #include #include #include intBoard[8][8]={0}; intHTry1[8]={2,-1,1,-2,2,1,-1,-2}; intHTry2[8]={1,2,2,1,-1,-2,-2,-1}; typedefstruct{ inti; intj; }PosType; typedefstruct{ intord; PosTypeseat; intdi; }SElemType; typedefstruct{ SElemType*base; SElemType*top; intstacksize; }SqStack; intInitStack(SqStack*s1){ (*s1).base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(! (*s1).base)exit(OVERFLOW); (*s1).top=(*s1).base; (*s1).stacksize=STACK_INIT_SIZE; return(OK); } SElemTypePop(SqStack*s,SElemTypee){ e=*--(*s).top; returne; } intPush(SqStack*s1,SElemTypee){ if((*s1).top-(*s1).base>=(*s1).stacksize){ (*s1).base=(SElemType*)realloc((*s1).base, ((*s1).stacksize+STACKINCREMENT)*sizeof (SElemType)); if(! (*s1).base)exit(OVERFLOW); (*s1).top=(*s1).base+(*s1).stacksize; (*s1).stacksize+=STACKINCREMENT; } *(*s1).top++=e; returnOK; } intStackEmpty(SqStack*s){ if((*s).base==(*s).top) return (1); else return(0); } intcurstep=0; intPass(PosTypes){ if((Board[s.i][s.j]==0)&&(s.i<=7)&&(s.i>=0)&&(s.j<=7)&&(s.j>=0)) return (1); else return(0); } PosTypeNextPos(PosTypes,inti){ s.i=s.i+HTry1[i-1]; s.j=s.j+HTry2[i-1]; return(s); } voidhorsesteps(intBoard[8][8],PosTypestart){ intk,j; SqStackS; SElemTypee; PosTypecurpos=start; InitStack(&S); do{ if(Pass(curpos)){ curstep++; Board[curpos.i][curpos.j]=curstep; e.seat=curpos; e.ord=curstep; e.di=1; Push(&S,e); if(curstep==64) break; else curpos=NextPos(curpos,1); }//if else{ if(! StackEmpty(&S)){ Pop(&S,e); if(e.di==8)Board[e.seat.i][e.seat.j]=0; while(e.di==8&&! StackEmpty(&S)){ e=Pop(&S,e); if(e.di==8)Board[e.seat.i][e.seat.j]=0; curstep=e.ord; }//while if(e.di<8){ e.di++; Push(&S,e); curpos=NextPos(e.seat,e.di); }//if }//if }//else }while(! StackEmpty(&S)); if(StackEmpty(&S)){ printf("马儿从这个初始位置不能踏遍棋盘\n"); printf("请按任意键推出本程序\n"); getchar(); exit(OVERFLOW); } for(j=0;j<8;j++){ printf("\n"); for(k=0;k<8;k++) printf("%3d",Board[j][k]); }//for }//函数结束 voidmain(){ intk,j; PosTypet; chara,b; printf("请输入马儿的初始位置\n"); fflush(stdin); scanf("%c%d,%d%c",&a,&k,&j,&b); t.i=k; t.j=j; printf("马儿走的路线为\n"); horsesteps(Board,t); } 8皇后: #include usingnamespacestd; staticintgEightQueen[8]={0},gCount=0; voidprint()//输出每一种情况下棋盘中皇后的摆放情况 { for(intouter=0;outer<8;outer++) { for(intinner=0;inner cout<<"#"; for(inner=gEightQueen[outer]+1;inner<8;inner++) cout<<""; cout< } cout<<"==========================\n"; } intcheck_pos_valid(intloop,intvalue)//检查是否存在有多个皇后在同一行/列/对角线的情况 { intindex; intdata; for(index=0;index { data=gEightQueen[index]; if(value==data) return0; if((index+data)==(loop+value)) return0; if((index-data)==(loop-value)) return0; } return1; } voideight_queen(intindex) { intloop; for(loop=0;loop<8;loop++) { if(check_pos_valid(index,loop)) { gEightQueen[index]=loop; if(7==index) { gCount++,print(); gEightQueen[index]=0; return; } eight_queen(index+1); gEightQueen[index]=0; } } } intmain(intargc,char*argv[]) { eight_queen(0); cout<<"total="< r
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序设计
![提示](https://static.bdocx.com/images/bang_tan.gif)