ImageVerifierCode 换一换
格式:DOCX , 页数:44 ,大小:25.40KB ,
资源ID:10306205      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10306205.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(程序设计课.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

程序设计课.docx

1、程序设计课汉诺塔:#include int hj(int a,int b, int c,int i)int t;if(i=1)printf(%d-%dn,a,c);elset=c;c=b;b=t;hj(a,b,c,i-1);printf(%d-%dn,a,b);t=a;a=c;c=t;t=b;b=c;c=t;hj(a,b,c,i-1);return 0;main()int a,b,c,i;a=1;b=2;c=3;printf(请输入汉诺塔的盘数);scanf(%d,&i);hj(a,b,c,i);return 0;快速排序:#include #include #define N 6int pa

2、rtition(int arr, int low, int high) int key; key = arrlow; while(lowhigh) while(low = key ) high-; if(lowhigh) arrlow+ = arrhigh; while( lowhigh & arrlow=key ) low+; if(lowhigh) arrhigh- = arrlow; arrlow = key; return low;void quick_sort(int arr, int start, int end) int pos; if (startend) pos = part

3、ition(arr, start, end); quick_sort(arr,start,pos-1); quick_sort(arr,pos+1,end); return;int main(void) int i; int arrN=32,12,7, 78, 23,45; printf(排序前 n); for(i=0;iN;i+) printf(%dt,arri); quick_sort(arr,0,N-1); printf(n 排序后 n); for(i=0; iN; i+) printf(%dt, arri); printf (n); system(pause); return 0;最长

4、公共子序列:#include#include#define N 20void LCSLength(int m,int n,char xN+1,char yN+1,char bN+1N+1);void LCS(int i,int j,char xN+1,char bN+1N+1);void main() int lx,ly; char XN+1,YN+1; char BN+1N+1; printf(请输入X序列:n); scanf(%s,X+1); printf(请输入Y序列:n); scanf(%s,Y+1); lx=strlen(X+1); printf(X序列的长度:); printf(%

5、dn,lx); ly=strlen(Y+1); printf(Y序列的长度:); printf(%dn,ly); LCSLength(lx,ly,X,Y,B); LCS(lx,ly,X,B);void LCSLength(int m,int n,char x,char y,char bN+1N+1) int i,j,s,t; int cN+1N+1; for (i=0;i=m;i+) ci0=0; for (i=0;i=n;i+) c0i=0; for (i=1;i=m;i+) for (j=1;j=cij-1) cij=ci-1j; bij=U; else cij=cij-1; bij=L;

6、 for (s=0;s=m;s+) for (t=0;t=n;t+) printf(%d,cst); printf(n); printf(length=%dn,cmn);void LCS(int i,int j,char x,char bN+1N+1) if (i=0 | j=0) return; if (bij=A) LCS(i-1,j-1,x,b); printf(%c,xi); else if (bij=U) LCS(i-1,j,x,b); else LCS(i,j-1,x,b);哈夫曼编码:#include #include #include #include #define M 10

7、0typedef struct Fano_Nodechar ch;float weight;FanoNodeM;typedef struct nodeint start;int end;struct node *next;LinkQueueNode;typedef structLinkQueueNode *front;LinkQueueNode *rear;LinkQueue;/建立队列void EnterQueue(LinkQueue *q,int s,int e)LinkQueueNode *NewNode;/生成新节点NewNode=(LinkQueueNode*)malloc(size

8、of( LinkQueueNode );if(NewNode!=NULL)NewNode-start=s;NewNode-end=e;NewNode-next=NULL;q-rear-next=NewNode;q-rear=NewNode;elseprintf(Error!);exit(-1);/按权分组void Divide(FanoNode f,int s,int *m,int e)int i;float sum,sum1;sum=0;for(i=s;i=e;i+)sum+=fi.weight;/*m=s;sum1=0;for(i=s;ifabs(sum-2*sum1-2*fi+1.wei

9、ght)?(i+1):*m;if(*m=i) break;void main()int i,j,n,max,m,hM;int sta,end;float w;char c,fcMM;FanoNode FN;LinkQueueNode *p;LinkQueue *Q;/初始化队QQ=(LinkQueue *)malloc(sizeof(LinkQueue);Q-front=(LinkQueueNode*)malloc(sizeof(LinkQueueNode);Q-rear=Q-front;Q-front-next=NULL;printf(t*FanoCoding*n);printf(Pleas

10、e input the number of node:);/输入信息scanf(%d,&n);/超过定义M,退出if(n=M)printf(=%d,M);exit(-1);i=1; /从第二个元素开始录入while(i=n)printf(%d weight and node:,i);scanf(%f %c,&FNi.weight,&FNi.ch);for(j=1;ji;j+)if(FNi.ch=FNj.ch)/查找重复printf(Same node!n); break;if(i=j)i+;/排序(降序)for(i=1;i=n;i+)max=i+1;for(j=max;j=n;j+)max=F

11、Nmax.weightFNj.weight?j:max;if(FNi.weightFNmax.weight)w=FNi.weight;FNi.weight=FNmax.weight;FNmax.weight=w;c=FNi.ch;FNi.ch=FNmax.ch;FNmax.ch=c;for(i=1;ifront-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

12、=sta;i=m;i+)fcihi=0;+hi;if(sta!=m)EnterQueue(Q,sta,m);elsefcstahsta=0;for(i=m+1;i=end;i+)fcihi=1;+hi;if(m=sta&(m+1)=end)/如果分组后首元素的下标与中间元素的相等,/并且和最后元素的下标相差为1,则编码码字字符串结束fcmhm=0;fcendhend=0;elseEnterQueue(Q,m+1,end);for(i=1;i=n;i+) /*打印编码信息*/printf(%c:,FNi.ch);printf(%sn,fci);system(pause);最短路径:#includ

13、e #include using namespace std; void Dijkstra(int n,int v,int dist,int prev,int *c) int maxint = 65535; bool *s = new booln; for (int i = 1; i = n; i+) disti = cvi; si = false; if (disti = maxint) previ = 0; else previ = v; distv = 0; sv = true; for (int i = 1; i n; i+) int temp = maxint; int u = v;

14、 for (int j = 1; j = n; j+) if (!sj) & (distj temp) u = j; temp = distj; su = true; for (int j = 1; j = n; j+) if (!sj) & (cuj maxint) int newdist = distu + cuj; if (newdist distj) distj = newdist; prevj = u; int main() int n,v,u; int q = 0; coutn; int *way = new intn + 1; int *c = new int *n + 1; f

15、or (int i = 1; i = n; i+) ci = new intn + 1; cout输入顶点的邻接矩阵:; for (int j = 1; j = n; j+) for (int t = 1; t cjt; int *dist = new int n; int *prev = new int n; coutvu; Dijkstra(n, v, dist, prev, c); cout最短路径从v u 的距离是:distuendl; int w = u; while (w != v) q+; wayq = prevw; w = prevw; cout= 1; j-) coutway

16、j; coutuendl; delete way; way=NULL; for (int i = 1; i = n; i+) delete ci; delete c; c=NULL; delete dist; dist=NULL; delete prev; prev=NULL; system(pause); return 0; 马踏棋盘:#define STACK_INIT_SIZE 100#define STACKINCREMENT 10#define OVERFLOW -2#define OK 1#include#include#includeint Board88=0;int HTry1

17、8=2,-1,1,-2,2,1,-1,-2;int HTry28=1,2,2,1,-1,-2,-2,-1;typedef structint i;int j;PosType;typedef structint ord;PosType seat;int di;SElemType;typedef structSElemType *base;SElemType *top;int stacksize;SqStack;int InitStack(SqStack *s1)(*s1).base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType);if(

18、!(*s1).base) exit(OVERFLOW);(*s1).top=(*s1).base;(*s1).stacksize=STACK_INIT_SIZE;return(OK);SElemType Pop(SqStack *s,SElemType e)e=*-(*s).top;return e;int Push(SqStack *s1,SElemType e)if(*s1).top-(*s1).base=(*s1).stacksize)(*s1).base=(SElemType *)realloc(*s1).base,(*s1).stacksize+STACKINCREMENT)*siz

19、eof(SElemType);if(!(*s1).base) exit(OVERFLOW);(*s1).top=(*s1).base+(*s1).stacksize;(*s1).stacksize+=STACKINCREMENT;*(*s1).top+=e;return OK;int StackEmpty(SqStack *s)if(*s).base=(*s).top)return(1);elsereturn(0);int curstep=0;int Pass(PosType s)if(Boards.is.j=0)&(s.i=0)&(s.j=0)return(1);elsereturn(0);

20、PosType NextPos(PosType s,int i)s.i=s.i+HTry1i-1;s.j=s.j+HTry2i-1;return(s);void horsesteps(int Board88,PosType start)int k,j;SqStack S;SElemType e;PosType curpos=start;InitStack(&S);doif(Pass(curpos)curstep+;Boardcurpos.icurpos.j=curstep;e.seat=curpos;e.ord=curstep;e.di=1;Push(&S,e);if(curstep=64)b

21、reak;elsecurpos=NextPos(curpos,1);/ifelseif(!StackEmpty(&S)Pop(&S,e);if(e.di=8) Boarde.seat.ie.seat.j=0;while(e.di=8&!StackEmpty(&S)e=Pop(&S,e);if(e.di=8) Boarde.seat.ie.seat.j=0;curstep=e.ord;/whileif(e.di8)e.di+;Push(&S,e);curpos=NextPos(e.seat,e.di);/if/if/elsewhile(!StackEmpty(&S);if(StackEmpty(

22、&S)printf(马儿从这个初始位置不能踏遍棋盘n);printf(请按任意键推出本程序n);getchar();exit(OVERFLOW);for(j=0;j8;j+)printf(n);for(k=0;k8;k+)printf(%3d,Boardjk);/ for/函数结束void main()int k,j;PosType t;char a,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皇后:#incl

23、udeusing namespace std;static int gEightQueen8=0,gCount=0;void print()/输出每一种情况下棋盘中皇后的摆放情况for(int outer=0;outer8;outer+)for(int inner=0;innergEightQueenouter;inner+)cout#;for( inner=gEightQueenouter+1;inner8;inner+)cout;coutendl;cout=n;int check_pos_valid(int loop,int value)/检查是否存在有多个皇后在同一行/列/对角线的情况i

24、nt index;int data;for(index=0;indexloop;index+)data=gEightQueenindex;if(value=data)return 0;if(index+data)=(loop+value)return 0;if(index-data)=(loop-value)return 0;return 1;void eight_queen(int index)int loop;for(loop=0;loop8;loop+)if(check_pos_valid(index,loop)gEightQueenindex=loop;if(7=index)gCount+,print();gEightQueenindex=0;return;eight_queen(index+1);gEightQueenindex=0;int main(int argc,char*argv)eight_queen(0);couttotal=gCountendl;r

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1