资源描述:
《c语言-简单小程序-简单算法.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、以下小程序都在TC2.0中运行通过,程序没来得及加注释。程序比较简单,都是一些初学c语言时写的程序,请通过运行看效果。5个字符串排序.c#include"stdio.h"#include"stdilb.h"voidpaixu(char*p[5]){inti,j;char*q;for(i=0;i<4;i++)for(j=0;j<4-i;j++)if(strcmp(p[j],p[j+1])>0){q=p[j];p[j]=p[j+1];p[j+1]=q;}}main(){char*p[5];inti;for(i=0;i<5;i++){p[i]=(char*)mallo
2、c(10);scanf("%s",p[i]);}paixu(p);for(i=0;i<5;i++)printf("%s",p[i]);}**********************************************************************************//21根常胜.c#include"stdio.h"#include"conio.h"stick(intleft){inttake;if(left==1){loop1:printf("Howmanystickdoyouwishtotake(1~1)?");flu
3、shall();if(scanf("%d",&take)!=1
4、
5、take>1
6、
7、take<=0){printf("Iserror!");gotoloop1;}printf("Youhavetakenthelaststick.***Youlose!");}else{loop:printf("Howmanystickdoyouwishtotake(1~4)?");flushall();if(scanf("%d",&take)!=1
8、
9、take>4
10、
11、take<=0){printf("Iserror!");gotoloop;}printf("%dst
12、ickleftinthepile.",left-take);printf("Computetake%dstick.",5-take);printf("%dstickleftinthepile.",left-5);stick(left-5);}}main(){system("cls");textbackground(GREEN);textcolor(YELLOW);clrscr();printf("*turn.Eachoneeachtimetakes1to4sticks.The*");printf("*onewhotakesthelaststick
13、willlosethegame.*");printf("***************************************************");printf(">>-------------------GameBegin--------------------");stick(21);printf(">>-------------------GameOver!--------------------");}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
14、&&&&&&&&&&&&&&&&&&&&&&&&//猜数字游戏#include"stdio.h"#include"stdlib.h"intyuan[4],shu[4];yuan_fen(intnum){yuan[0]=num/1000;yuan[1]=num/100%10;yuan[2]=num/10%10;yuan[3]=num%10;}shu_fen(intnum){shu[0]=num/1000;shu[1]=num/100%10;shu[2]=num/10%10;shu[3]=num%10;}intshudui_panduan(){intshu1[4],
15、shudui=0,i,j,t=0,q,biao=0;for(i=0;i<4;i++)shu1[i]=-1;for(i=0;i<4;i++){for(j=0;j<4;j++){if(shu[i]==yuan[j]){biao=0;for(q=0;q16、==yuan[i])we