c语言简单小程序简单算法.docx
- 文档编号:6400772
- 上传时间:2023-01-06
- 格式:DOCX
- 页数:28
- 大小:19.42KB
c语言简单小程序简单算法.docx
《c语言简单小程序简单算法.docx》由会员分享,可在线阅读,更多相关《c语言简单小程序简单算法.docx(28页珍藏版)》请在冰豆网上搜索。
c语言简单小程序简单算法
以下小程序都在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*)malloc(10);
scanf("%s",p[i]);
}
paixu(p);
for(i=0;i<5;i++)
printf("%s\n",p[i]);
}_
**********************************************************************************
//21根常胜.c
#include"stdio.h"
#include"conio.h"
stick(intleft)
{
inttake;
if(left==1)
{
loop1:
printf("Howmanystickdoyouwishtotake(1~1)?
");
flushall();
if(scanf("%d",&take)!
=1||take>1||take<=0)
{
printf("Iserror!
\n");
gotoloop1;
}
printf("Youhavetakenthelaststick.\n***Youlose!
\n");
}
else
{
loop:
printf("Howmanystickdoyouwishtotake(1~4)?
");
flushall();
if(scanf("%d",&take)!
=1||take>4||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");
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//猜数字游戏
#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],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;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;i<4;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(manin>9999||manin<1000)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; } &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //九宫格.c main() { inta,b,c,d,i,j; ints[30][30];/*定义一个足够大的数组*/ printf("shuruyigejishu: "); scanf("%d",&b); if(b%2! =0)/*输入一个数,不是偶数才执行下面的代码*/ { for(i=0;i for(j=0;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+1>b-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+1>b-1) c=c+1; else { c=b-1; d=d+1; } } s[c][d]=a; } /*下面输出数组*/ for(i=0;i { for(j=0;j { printf("%d\t",s[i][j]);/*这里用了\t所以不要输入的数太大,不然输出对不齐*/ } printf("\n"); } } } &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //输出菱形数组.c #include #include main() { 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));j<19;j++)printf("%c",s[j]); printf("\n"); } } *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //商人和强盗过河游戏.C #include"stdio.h" #include"conio.h" intinman=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(inman>2||inman>thisman) { 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(inret>2||inret>thisret) { 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(inman>2||inman>thatman) { 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(inret>2||inret>thatret) { gotoxy(15,20); printf("Inputerror! Pressanykeytorestart..."); flushall(); getchar(); gotoxy(15,20); printf(""); gotol4; } } intthis_panduan() { intthisman1,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 { thisman=thisman1; thisret=thisret1; thatman=thatman1; thatret=thatret1; return0; } else { if(thatman! =0&&thatman { thisman=thisman1; thisret=thisret1; thatman=thatman1; thatret=thatret1; return0; } elsereturn1; } } intthat_panduan() { intthisman1,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 { thisman=thisman1; thisret=thisret1; thatman=thatman1; thatret=thatret1; return0; } else { if(thatman! =0&&thatman { thisman=thisman1; thisret=thisret1; thatman=thatman1; thatret=thatret1; return0; } elsereturn1; } } t_printf() { inti; gotoxy(12,15); printf(""); gotoxy(12,15); for(i=0;i printf("Man"); gotoxy(12,16); printf(""); gotoxy(12,16); for(i=0;i printf("Retainer"); gotoxy(42,15); printf(""); gotoxy(42,15); for(i=0;i printf("Man"); gotoxy(42,16); printf(""); gotoxy(42,16); for(i=0;i printf("Retainer"); } main() { intj=0; system("cls"); textbackground(BLUE); textcolor(YELLOW); clrscr(); printf("*******************************************************************************\n"); printf("**\n"); printf("*^_^Welcometothegame! ^_^*\n"); printf("**\n"); printf("*GameRules: *\n"); printf("*3mentake3retainersandgoodstopasstheriver,theboatcarrys*\n"); printf("*2personseachtime.Inpassingtheriver,atanybanknumberof*\n"); printf("*menmustbemorethanthenumberoftheretainers,ortheretainers*\n"); printf("*willkillthemenandtakethegoods.*\n"); printf("*Gameoperations: (1)Inputthenumberofmenandretainersinturn;*\n"); printf("* (2)Inputerrorkeys,thegamewillrestart.*\n"); printf("**\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("Pressanykeytostartgame.(Qkeytoquit)..."); if(getchar()=='Q')return0; w1: j=0; thisman=3; thatman=0; thisret=3; thatret=0; gotoxy(16,17); printf(""); gotoxy(10,14); printf("Thisbank"); gotoxy(40,14); printf("Thatbank"); gotoxy(12,15); printf("ManManMan"); gotoxy(12,16); printf("RetainerRetainerRetainer"); gotoxy(42,15); printf(""); gotoxy(42,16); printf(""); while(thisman! =0||thisret! =0) { t1: gotoxy(27,14); printf("--->"); if(thisman! =0)this_inm
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 简单 程序 算法