C语言简单小程序简单算法_第1页
C语言简单小程序简单算法_第2页
C语言简单小程序简单算法_第3页
C语言简单小程序简单算法_第4页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

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

文档简介

1、以下小程序都在 TC2.0 中运行通过,程序没来得及加注释。程序比较简单,都是一些初学 c 语言时写的程序,请通过运行看效果。5 个字符串排序.c#include "stdio.h"#include "stdilb.h"void paixu(char *p5)int i,j;char *q;for(i=0;i<4;i+)for(j=0;j<4-i;j+)if(strcmp(pj,pj+1)>0)q=pj;pj=pj+1;pj+1=q;main()char *p5;int i;for(i=0;i<5;i+)pi=(char *)ma

2、lloc(10);scanf("%s",pi);paixu(p);for(i=0;i<5;i+)printf("%sn",pi);*/21 根 常胜 .c#include "stdio.h"#include "conio.h"stick(int left)int take;if(left=1)loop1:printf("Howmany stick do you wish to take(11)?");flushall();if(scanf("%d",&take)

3、!=1|take>1|take<=0)printf(" Is error!n");goto loop1;printf(" You have taken the last stick.n* * * You lose!n");elseloop: printf("Howmany stick do you wish to take(14)?");flushall();if(scanf("%d",&take)!=1|take>4|take<=0)printf(" Is error!n

4、");goto loop;printf(" %d stick left in the pile.n",left-take);printf(" Compute take %d stick.n",5-take);printf(" %d stick left in the pile.n",left-5);stick(left-5);main()system("cls");textbackground(GREEN);textcolor(YELLOW);clrscr();printf("*turn.Eac

5、hone each time takes 1 to 4 sticks. The *n");printf("*onewho takes the last stick willlose the game. *n");printf("*n");printf(">>-GameBegin-n");stick(21);printf(">>-GameOver!-n");&&&&&&&&&&&&&

6、;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&am

7、p;&&&&&&&&&&&&/猜数字游戏#include "stdio.h"#include "stdlib.h"int yuan4,shu4;yuan_fen(int num)yuan0=num/1000;yuan1=num/100%10;yuan2=num/10%10;yuan3=num%10;shu_fen(int num)shu0=num/1000;shu1=num/100%10;shu2=num/10%10;shu3=num%10;int shudu

8、i_panduan()int shu14,shudui=0,i,j,t=0,q,biao=0;for(i=0;i<4;i+)shu1i=-1;for(i=0;i<4;i+)for(j=0;j<4;j+)if(shui=yuanj)biao=0;for(q=0;q<t;q+)if(shui=shu1q)biao=1;break;if(biao=0)shu1t=shui;t+;shudui+;return shudui;int weidui_panduan()int weidui=0,i;for(i=0;i<4;i+)if(shui=yuani)weidui+;ret

9、urn weidui;main()int dui,manin;g2:system("cls");printf("*n");printf("*Input10000 Eixt!*n");printf("*n");dui=0;randomize();yuan_fen(1000+random(9000);gotoxy(3,5);printf("Your Input");gotoxy(20,5);printf("jige shu dui");gotoxy(37,5);printf(&q

10、uot;jiwei dui");while(1)g1:gotoxy(1,6+dui);printf("");gotoxy(3,6+dui);if(scanf("%d",&manin)!=1)goto g1;if(manin=10000)break;if(manin>9999|manin<1000)goto g1;shu_fen(manin);gotoxy(23,6+dui);printf("%d",shudui_panduan();gotoxy(40,6+dui);printf("%d"

11、;,weidui_panduan();if(weidui_panduan()!=4)dui+;gotog1;else break;gotoxy(10,7+dui);printf("yigong cai le %d ci!",dui+1);gotoxy(10,8+dui);printf("nixuan ze ji xu?(y/n)");flushall();if(getchar()='y')goto g2;&&&&&&&&&&&&&&am

12、p;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&am

13、p;&&&&&&&&&&&&&&&&/九宫格 .cmain()int a,b, c, d,i,j;int s3030 ;/*定义一个足够大的数组printf("shu ru yi ge ji shu:");scanf("%d",&b);*/if(b%2!=0)/*输入一个数,不是偶数才执行下面的代码*/for ( i = 0; i < b; i+)/* 将数组里赋初值0,以便后面的比较*/for ( j = 0;

14、j < b; j+)sij = 0;d = (b - 1) / 2;c = 0;/* 下面是向数组里填数,填成魔方s0d = 1;for (a = 2; a <= b * b; a+)*/if (c - 1 >= 0)if (d + 1 > b - 1)c = c - 1;d = 0;elseif (sc - 1 d + 1 != 0)c = c + 1;elsec = c - 1;d = d + 1;elseif (d + 1 > b - 1)c = c + 1;elsec = b - 1;d = d + 1;sc d = a;/* 下面输出数组 */for (

15、i = 0; i < b; i+)for (j = 0; j < b; j+)printf("%dt",sij);/*这里用了 t 所以不要输入的数太大,不然输出对不齐*/printf("n");&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

16、amp;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&/输出菱形数组 .c#

17、include <stdio.h>#include <math.h>main()int i,j,n;char s20="1234567890987654321"printf("nn");n=19;for(i=-n/2;i<=n/2; i+)for(j=0;j<=abs(i);j+)printf("%c",'');for(j=0;j<=n/2-abs(i);j+)printf("%c",sj);for(j=19-(n/2-abs(i);j<19;j+)p

18、rintf("%c",sj);printf("n");*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &

19、amp;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& /商人和强盗过河游戏 .C#include "stdio.h"#include "conio.h"int inman=0,inret=0,thisman=3,thisret=3,thatman=0,thatret=0;this_i

20、nmans()l1:gotoxy(20,17);printf(" "); gotoxy(20,17);printf("Inputthe number of men to that bank:");flushall();if(scanf("%d",&inman)!=1)goto l1;gotoxy(15,20);printf(" "); if(inman>2|inman>thisman)gotoxy(15,20);printf("Inputerror!Press any key to r

21、estart.");flushall();getchar();gotoxy(15,20);printf("goto l1;");this_inrets()l2:gotoxy(20,17);printf("");gotoxy(20,17);printf("Inputthe number of retainer to that bank:");flushall();if(scanf("%d",&inret)!=1)goto l2;gotoxy(15,20);printf("");i

22、f(inret>2|inret>thisret)gotoxy(15,20);printf("Inputerror!Press any key to restart.");flushall();getchar();gotoxy(15,20);printf("");goto l2;that_inmans()l3:gotoxy(20,17);printf("gotoxy(20,17);printf("Inputthe number of men to this bank:");flushall();");if

23、(scanf("%d",&inman)!=1)goto l3;gotoxy(15,20);printf(" "); if(inman>2|inman>thatman)gotoxy(15,20);printf("Inputerror!Press any key to restart.");flushall();getchar();gotoxy(15,20);printf("goto l3;");that_inrets()l4:gotoxy(20,17);printf("");g

24、otoxy(20,17);printf("Inputthe number of retainerto this bank:");flushall();if(scanf("%d",&inret)!=1)goto l4;gotoxy(15,20);printf("");if(inret>2|inret>thatret)gotoxy(15,20);printf("Inputerror!Press any key to restart.");flushall();getchar();gotoxy(15,

25、20);printf(" "); goto l4;int this_panduan()int thisman1,thisret1,thatman1,thatret1;thisman1=thisman;thisret1=thisret;thatman1=thatman;thatret1=thatret;thisman=thisman-inman;thisret=thisret-inret;thatman=thatman+inman;thatret=thatret+inret;if(thisman!=0&&thisman<thisret)thisman=t

26、hisman1;thisret=thisret1;thatman=thatman1;thatret=thatret1;return 0;elseif(thatman!=0&&thatman<thatret)thisman=thisman1;thisret=thisret1;thatman=thatman1;thatret=thatret1;return 0;else return 1;int that_panduan()int thisman1,thisret1,thatman1,thatret1;thisman1=thisman;thisret1=thisret;tha

27、tman1=thatman;thatret1=thatret;thisman=thisman+inman;thisret=thisret+inret;thatman=thatman-inman;thatret=thatret-inret;if(thisman!=0&&thisman<thisret)thisman=thisman1;thisret=thisret1;thatman=thatman1;thatret=thatret1;return 0;elseif(thatman!=0&&thatman<thatret)thisman=thisman1

28、;thisret=thisret1;thatman=thatman1;thatret=thatret1;return 0;else return 1;t_printf()int i;gotoxy(12,15);printf("");gotoxy(12,15);for(i=0;i<thisman;i+)printf("Man");gotoxy(12,16);printf("");gotoxy(12,16);for(i=0;i<thisret;i+)printf("Retainer");gotoxy(42,

29、15);printf("");gotoxy(42,15);for(i=0;i<thatman;i+)printf("Man");gotoxy(42,16);printf("");gotoxy(42,16);for(i=0;i<thatret;i+)printf("Retainer");main()int j=0;system("cls");textbackground(BLUE);textcolor(YELLOW);clrscr();printf("*n");pr

30、intf("*n");printf("*_Welcometothegame!_*n");printf("*n");printf("*GameRules:*n");printf("*printf("*3 men take 3 retainers and goods to pass the river, the boat carrys*n");2persons each time.Inpassing the river,at anybanknumberof*n");printf(

31、"*men must be more than the numberof the retainers, or the retainers*n");printf("*willkillthemenandtakethegoods.*n");printf("*Game operations :(1)Inputthe number ofmen and retainers inturn;*n");printf("*(2) Input error keys, the game willrestart.*n");printf(&q

32、uot;*n");printf("* *n");printf("*n");printf("*n");printf("*n");printf("*n");printf("*n");printf("*n");printf("*n");printf("*n");printf("*n");printf("*n");gotoxy(16,17);printf("Pres

33、s any key to start game.(Q key to quit).");if(getchar()='Q')return 0;w1:j=0;thisman=3;thatman=0;thisret=3;thatret=0;gotoxy(16,17);printf(" "); gotoxy(10,14);printf("Thisbank");gotoxy(40,14);printf("That bank");gotoxy(12,15);printf("Man Man Man");g

34、otoxy(12,16);printf("Retainer Retainer Retainer");gotoxy(42,15);printf("");gotoxy(42,16);printf("");while(thisman!=0|thisret!=0)t1:gotoxy(27,14);printf("->");if(thisman!=0) this_inmans();else inman=0;if(thisret!=0&&inman!=2)this_inrets();else inret=

35、0;if(inret+inman>2)gotoxy(15,20);printf("Inputerror!Press any key to restart.");flushall();getchar();gotoxy(15,20);printf("goto t1;");if(this_panduan()!=1)gotoxy(15,20);printf("");gotoxy(15,20);printf("Theman were killed.Press any key to restart.");flushall

36、();getchar();gotoxy(15,20);printf("");j=1;break;t_printf();t2:if(thisman=0&&thisret=0)break;gotoxy(27,14);printf("<-");if(thatman!=0) that_inmans();if(thatret!=0&&inman!=2)that_inrets();if(inret+inman>2)gotoxy(15,20);printf("");gotoxy(15,20);printf

37、("Inputerror!Press any key to restart.");goto t2;if(that_panduan()!=1)gotoxy(15,20);printf(" "); gotoxy(15,20);printf("Theman were killed.Press any key to restart.");flushall();getchar();gotoxy(15,20);printf("");j=1;break;t_printf();if(j=1) goto w1;elsegotoxy(

38、15,20);printf(" "); gotoxy(15,20);printf("Congratulations!Mission Completed!Any key to quit.");flushall();getchar();return 0;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

39、&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

40、&&&&&&字符倒置删除 .C#include <stdio.h>char *fan(char *p)char *a,*d,b;a=p;d=p;while(*p!='0')p+;p-;for(;a<p;a+,p-)b=*a;*a=*p;*p=b;return d;char *delect(char *p,char c)char *a,*d;a=p;d=p;while(*p!='0')if(*p!=c)*a=*p;a+;p+;*a='0'return d;main()char *p,

41、*q,ch;int c;doprintf("*n");printf("1. Reverse a string.n");printf("2. Delete a character in a string.n");printf("*n");printf("Please input your choice: n");scanf("%d",&c);switch(c)case1:printf("Enter a string: n");scanf("

42、%s",p);q=fan(p);printf("Reversed string is:%s n",q);break;case2:printf("Enter a string:n");scanf("%s",p);printf("Enter a character you want to delete: n");flushall();scanf("%c",&ch);q=delect(p,ch);printf("Deleted string is:%sn",q);

43、break;printf("Wouldyou like to continue? (y/n):");/*flushall();*/scanf("%c",&ch);printf("n");while(ch='y');&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

44、amp;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

45、amp;&&&有序二叉树 .c#include "stdio.h"#include "malloc.h"#define LEN sizeof(struct ercha)#define NULL 0struct erchaint data;struct ercha *lchild;struct ercha *rchild;struct ercha *head;add(struct ercha *p,struct ercha *q)if(p->data>q->data)if(p->lchild=NULL)p-&

46、gt;lchild=q;elseadd(p->lchild,q);elseif(p->rchild=NULL)p->rchild=q;elseadd(p->rchild,q);bianli( struct ercha *p)if(p!=NULL)bianli(p->lchild);printf("t%d",p->data);bianli(p->rchild);main()int pr_num,i=0;struct ercha *p;while(1)g1:flushall();p=(struct ercha *)malloc(LEN)

47、;if(scanf("%d",&pr_num)!=1)goto g1;if(pr_num=-1111) break;p->data=pr_num;p->lchild=NULL;p->rchild=NULL;if(i=0) head=p;i+;continue;add(head,p);bianli(head);$二叉树 .c#include "stdio.h"#include "conio.h"#include "malloc.h"#define LEN sizeof(struct ercha)#define NULL 0struct ercha *head;struct erchachar data;struct ercha * rch;struct ercha * lch;qianbianli(struct ercha *p)if(p!=NULL)printf("%c",p->data);qianbianli(p->lch);qianbianli(p->rch);zhongbianli(struct ercha *p)if(p!=NULL)zhongb

温馨提示

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

评论

0/150

提交评论