您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业文档 > c语言-简单小程序-简单算法
以下小程序都在TC2.0中运行通过,程序没来得及加注释。程序比较简单,都是一些初学c语言时写的程序,请通过运行看效果。5个字符串排序.c#includestdio.h#includestdilb.hvoidpaixu(char*p[5]){inti,j;char*q;for(i=0;i4;i++)for(j=0;j4-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;i5;i++){p[i]=(char*)malloc(10);scanf(%s,p[i]);}paixu(p);for(i=0;i5;i++)printf(%s\n,p[i]);}**********************************************************************************//21根常胜.c#includestdio.h#includeconio.hstick(intleft){inttake;if(left==1){loop1:printf(Howmanystickdoyouwishtotake(1~1)?);flushall();if(scanf(%d,&take)!=1||take1||take=0){printf(Iserror!\n);gotoloop1;}printf(Youhavetakenthelaststick.\n***Youlose!\n);}else{loop:printf(Howmanystickdoyouwishtotake(1~4)?);flushall();if(scanf(%d,&take)!=1||take4||take=0){printf(Iserror!\n);gotoloop;}printf(%dstickleftinthepile.\n,left-take);printf(Computetake%dstick.\n,5-take);printf(%dstickleftinthepile.\n,left-5);stick(left-5);}}main(){system(cls);textbackground(GREEN);textcolor(YELLOW);clrscr();printf(*turn.Eachoneeachtimetakes1to4sticks.The*\n);printf(*onewhotakesthelaststickwilllosethegame.*\n);printf(***************************************************\n);printf(-------------------GameBegin--------------------\n);stick(21);printf(-------------------GameOver!--------------------\n);}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//猜数字游戏#includestdio.h#includestdlib.hintyuan[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],shudui=0,i,j,t=0,q,biao=0;for(i=0;i4;i++)shu1[i]=-1;for(i=0;i4;i++){for(j=0;j4;j++){if(shu[i]==yuan[j]){biao=0;for(q=0;qt;q++){if(shu[i]==shu1[q]){biao=1;break;}}if(biao==0){shu1[t]=shu[i];t++;shudui++;}}}}returnshudui;}intweidui_panduan(){intweidui=0,i;for(i=0;i4;i++){if(shu[i]==yuan[i])weidui++;}returnweidui;}main(){intdui,manin;g2:system(cls);printf(**********************************************\n);printf(*************Input10000Eixt!****************\n);printf(**********************************************\n);dui=0;randomize();yuan_fen(1000+random(9000));gotoxy(3,5);printf(YourInput);gotoxy(20,5);printf(jigeshudui);gotoxy(37,5);printf(jiweidui);while(1){g1:gotoxy(1,6+dui);printf();gotoxy(3,6+dui);if(scanf(%d,&manin)!=1)gotog1;if(manin==10000)break;if(manin9999||manin1000)gotog1;shu_fen(manin);gotoxy(23,6+dui);printf(%d,shudui_panduan());gotoxy(40,6+dui);printf(%d,weidui_panduan());if(weidui_panduan()!=4){dui++;gotog1;}elsebreak;}gotoxy(10,7+dui);printf(yigongcaile%dci!,dui+1);gotoxy(10,8+dui);printf(nixuanzejixu?(y/n));flushall();if(getchar()=='y')gotog2;}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//九宫格.cmain(){inta,b,c,d,i,j;ints[30][30];/*定义一个足够大的数组*/printf(shuruyigejishu:);scanf(%d,&b);if(b%2!=0)/*输入一个数,不是偶数才执行下面的代码*/{for(i=0;ib;i++)/*将数组里赋初值0,以便后面的比较*/for(j=0;jb;j++)s[i][j]=0;d=(b-1)/2;c=0;/*下面是向数组里填数,填成魔方*/s[0][d]=1;for(a=2;a=b*b;a++){if(c-1=0){if(d+1b-1){c=c-1;d=0;}else{if(s[c-1][d+1]!=0)c=c+1;else{c=c-1;d=d+1;}}}else{if(d+1b-1)c=c+1;else{c=b-1;d=d+1;}}s[c][d]=a;}/*下面输出数组*/for(i=0;ib;i++){for(j=0;jb;j++){printf(%d\t,s[i][j]);/*这里用了\t所以不要输入的数太大,不然输出对不齐*/}printf(\n);}}}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//输出菱形数组.c#includestdio.h#includemath.hmain(){inti,j,n;chars[20]=1234567890987654321;printf(\n\n);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,s[j]);for(j=19-(n/2-abs(i));j19;j++)printf(%c,s[j]);printf(\n);}}*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//商人和强盗过河游戏.C#includestdio.h#includeconio.hintinman=0,inret=0,thisman=3,thisret=3,thatman=0,thatret=0;this_inmans(){l1:gotoxy(20,17);printf();gotoxy(20,17);printf(Inputthenumberofmentothatbank:);flushall();if(scanf(%d,&inman)!=1)gotol1;gotoxy(15,20);printf();if(inman2||inmanthisman){gotoxy(15,20);printf(Inputerror!Pressanykeytorestart...);flushall();getchar();gotoxy(15,20);printf();gotol1;}}this_inrets(){l2:gotoxy(20,17);printf();gotoxy(20,17);printf(Inputthenumberofretainertothatbank:);flushall();if(scanf(%d,&inret)!=1)gotol2;gotoxy(15,20);printf();if(inret2||inretthisret){gotoxy(15,20);printf(Inputerror!Pressanykeytorestart...);flushall();getchar();gotoxy(15,20);printf();gotol2;}}that_inmans(){l3:gotoxy(20,17);printf();gotoxy(20,17);printf(Inputthenumberofmentothisbank:);flushall();if(scanf(%d,&inman)!=1)gotol3;gotoxy(15,20);printf();if(inman2||inmanthatman){gotoxy(15,20);printf(Inputerror!Pressanykeytorestart...);flushall();getchar();gotoxy(15,20);printf();gotol3;}}that_inrets(){l4:gotoxy(20,17);printf();gotoxy(20,17);printf(Inputthenumberofretainertothisbank:);flushall();if(scanf(%d,&inret)!=1)gotol4;gotoxy(15,20);printf();if(inret2||inretthatret){gotoxy(15,20);printf(Inputerror!Pressanykeytorestart...);
本文标题:c语言-简单小程序-简单算法
链接地址:https://www.777doc.com/doc-3369227 .html