c语言比赛-商人过河源代码.doc_第1页
c语言比赛-商人过河源代码.doc_第2页
c语言比赛-商人过河源代码.doc_第3页
c语言比赛-商人过河源代码.doc_第4页
c语言比赛-商人过河源代码.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include#includevoid print() printf(*n); printf(* *n); printf(* welcome to the game *n); printf(* *n); printf(* Game rules: *n); printf(* 3man and 3rateiners and goods to pass *n); printf(*the river,the number of rateiners not more *n); printf(*over the number mans in the passing river. *n); printf(* Game operrations: *n); printf(* 1.input the number of men and rateiners *n); printf(* in turn *n); printf(* 2.input error keys,the game will restart*n); printf(*n);void began()/游戏开始 char ch; printf(nn); printf(press and key to start the game (Q key to quit).n); scanf(%c,&ch);if(ch=Q|ch=q) exit(0);/退出键void xianshi(char *a,char *b)/显示过河的动态 int ax=0,ay=0,bx=0,by=0;/ax ay代表原始岸边的人数,bx by代表现在岸那边人数 int i; for(i=0;i6;i+) if(*(a+i)=M)/岸这边商人数 ax+; if(*(a+i)=S)/岸这边仆人数 ay+; if(*(b+i)=M)/岸那边商人数 bx+; if(*(b+i)=S)/岸那边仆人数 by+; printf(this bankn);/岸边状况 for(i=1;iax;i+) printf(mann); printf(n); for(i=1;iay;i+) printf(rateinersn); printf(nn); printf(this bankn); for(i=1;ibx;i+) printf(mann); printf(n); for(i=1;iby;i+) printf(rateinersn); printf(nn);if(ax=0&ay=0&bx=3&by=3)/商人和仆人都运送过河 printf(congrarulation!You have finished the game!n);/游戏完成 exit(0);/退出游戏void pan(int ax,int ay,int bx,int by)/判断过河的商人与仆人是否安全即就是商人人数不少于仆人数 if(axay|bxby) printf(the men are killed!game overn); exit(0);main() int i,x=0,y=0; int ax=3,ay=3,bx=0,by=0; char a6,b6; system(color 1E); print(); began(); print();for(i=0;i3;i+)/M代表商人,s代表仆人 ai=M;for(i=3;i6;i+) ai=S;for(i=0;i6;i+)/游戏开始前的动态 即是3商人和3仆人在等待 bi=0;xianshi(a,b);/开始前动态do printf(please input number of man to that bank:);/输入商人乘船数 scanf(%d,&x); while(x2)/判定过河人数是否正确即不能超过2人或无人 printf(the wrong number ,please enter again:); scanf(%d,&x); print(); xianshi(a,b); printf(please input number of rateiners to that bank:);/输入仆人乘船数 scanf(%d,&y); while(y2|x+y2)/判定过河人数是否正确即不能超过2人或无人 printf(the wrong number ,please enter again:); scanf(%d,&y); ax=ax-x;ay=ay-y;pan(ax,ay,bx,by);bx=bx+x;by=ay+y;/一次过后,两边商人与仆人的人数print();for(i=0;i6;i+)ai=0;bi=0;for(i=0;iax;i+) ai=M;for(i=3;i3+ay;i+) ai=S;for(i=0;ibx;i+) bi=M;for(i=3;i3+by;i+) bi=S;xianshi(a,b); printf(please input number of man to this bank:); scanf(%d,&x); while(x2) printf(the wrong number ,please enter again:); scanf(%d,&x); print(); xianshi(a,b); printf(please input number of rateiners to that bank:);/输入仆人乘船数 scanf(%d,&y); while(y2|x+y2)/判定过河人数是否正确即不能超过2人或无人 printf(the wrong number ,please enter again:); scanf(%d,&y); bx=bx-x;by=by-y;pan(ax,ay,bx,by);ax=ax+x;ay=ay+y;/一次过后,两边商人与仆人的人数print();for(i=0;i6;i+)ai=0;bi=0;for(i=0;iax;i+) ai=M;for(i=3;i3+ay;i+) ai=S;for(i=0;ibx;i+) bi=M;for(i=3;i3+by;i+) bi=S;xianshi(a,b); printf(please input number of

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论