您的位置:

关于c语言故事,一个C语言程序

本文目录一览:

C语言 韩信点兵

for(i=5;;i+=5)

{if((i+1)%6==5(i+1)%7==4(i+1)%11==10)

{printf("韩信有%d个兵",i+1));break;}

}

小青蛙爬井 c语言

你错在把scanf语句放在了while里面,而while的脱离条件又是u、n、d至少有一个为0,导致

你输入一组正常数据时会一直在循环中出不来,循环的内容就是重复要求输入n、u、d的值。

可以这样改:

#includestdio.h

int main()

{

int n,u,d,t,s,T=0;

scanf("%d,%d,%d",n,u,d);

while(1)

if((n||u||d)!=0)

break;

t=0,s=0;

while(sn)

{

t=t+1;

s=(u-d)*t+u;

}

T=2*t+1;

printf("结果是:%d\n",T);

return 0;

}

运行结果如图:

母牛的故事 C语言

这样分析才是对的,楼上说的没错,后面的还会产仔。

#includestdio.h

#define N 55

main()

{

int a,b[10],n=0;

int i;

printf("Sample Input\n");

while(scanf("%d",a)!=EOFa0)

{

if(a55)continue;

b[n++]=a;

}

printf("Sample Output\n");

for(i=0;in;i++)

printf("%d\n",f(b[i]));

getch();

}

int f(int n)

{

if(n=4)return n;

else return f(n-1)+(f(n-1)-3)+1;

//跟前一年相比,已经出娘胎的母牛只有3只不产仔,

//至于‘1’么:按题目的要求,好像老母牛光产仔,不算数的。

}

c语言爱因斯坦 谁养鱼

#includestdio.h

char*COL[]={NULL,"红","黄","绿","蓝","白"};

char*PAD[]={NULL,"狗","猫","鱼","鸟","马"};

char*DRK[]={NULL,"茶 ","牛奶","咖啡","啤酒","水 "};

char*GUO[]={NULL,"挪威","英国","德国","丹麦","瑞典"};

char*SMK[]={NULL,"Blends","BlueMaster","Dunhill","Pall Mall","Prince"};

struct{ char guojia, color, pads, drink, smoke; } aa[5];

int OK(void)

{

int i,j;

for(i=0;i5;i++)

if(aa[i].guojia==2 aa[i].color==1)goto next1;

return 0;

next1:

for(i=0;i5;i++)

if(aa[i].guojia==5 aa[i].pads==1)goto next2;

return 0;

next2:

for(i=0;i5;i++)

if(aa[i].guojia==4 aa[i].drink==1)goto next3;

return 0;

next3:

for(i=0;i5;i++)

if(aa[i].color==3)goto next3_1;

return 0;

next3_1:

for(j=i+1;j5;j++)

if(aa[j].color==5)goto next4;

return 0;

next4:

for(i=0;i5;i++)

if(aa[i].color==3 aa[i].drink==3)goto next5;

return 0;

next5:

for(i=0;i5;i++)

if(aa[i].smoke==4 aa[i].pads==4)goto next6;

return 0;

next6:

for(i=0;i5;i++)

if(aa[i].color==2 aa[i].smoke==3)goto next7;

return 0;

next7:

for(i=0;i5;i++)

if(aa[i].guojia==3 aa[i].smoke==5)goto next10;

return 0;

next10:

for(i=0;i5;i++)

if(aa[i].smoke==2 aa[i].drink==4)goto next11;

return 0;

next11:

for(i=0;i5;i++)

if(aa[i].smoke==3)goto next12_1;

next12_1:

if(i==0){if(aa[1].pads==5)goto next13;}

else if(i==4){if(aa[3].pads==5)goto next13;}

else if(aa[i-1].pads==5||aa[i+1].pads==5)goto next13;

return 0;

next13:

for(i=0;i5;i++)

if(aa[i].smoke==1)goto next13_1;

next13_1:

if(i==0){if(aa[1].pads==2)goto next14;}

else if(i==4){if(aa[3].pads==2)goto next14;}

else if(aa[i-1].pads==2||aa[i+1].pads==2)goto next14;

return 0;

next14:

for(i=0;i5;i++)

if(aa[i].smoke==1)goto next14_1;

next14_1:

if(i==0){if(aa[1].drink==5)goto next15;}

else if(i==4){if(aa[3].drink==5)goto next15;}

else if(aa[i-1].drink==5||aa[i+1].drink==5)goto next15;

return 0;

next15:

return 1;

}

char color[4]={1,2,3,5};

char drink[4]={1,3,4,5};

char guojia[4]={2,3,4,5};

char smoke[]={1,2,3,4,5};

char pads[5]={1,2,3,4,5};

int rot(char a[],int n)

{ int i,j,k,t;

for(k=n-1;k0;k--)

if(a[k-1]a[k])break;

if(k==0)

{ for(i=0,j=n-1;ij;i++,j--)

{char t=a[i];

a[i]=a[j];

a[j]=t;

}

return 0;

}

t=a[k-1];i=k;

for(j=k+1;jn;j++)

if(ta[j]a[j]a[i])i=j;

a[k-1]=a[i];a[i]=t;

for(i=k;in-1;i++)

for(j=k;jn-1+k-i;j++)

if(a[j]a[j+1])

{ t=a[j];

a[j]=a[j+1];

a[j+1]=t;

}

return 1;

}

int main()

{ int i,j,k,ans=0;

int i1,i2,i3,i4,i5;

aa[1].color=4;

aa[2].drink=2;

aa[0].guojia=1;

for(i1=0;i124;i1++){

aa[0].color=color[0];

aa[2].color=color[1];

aa[3].color=color[2];

aa[4].color=color[3];

rot(color,4);

for(i2=0;i224;i2++){

aa[0].drink=drink[0];

aa[1].drink=drink[1];

aa[3].drink=drink[2];

aa[4].drink=drink[3];

rot(drink,4);

for(i3=0;i324;i3++){

aa[1].guojia=guojia[0];

aa[2].guojia=guojia[1];

aa[3].guojia=guojia[2];

aa[4].guojia=guojia[3];

rot(guojia,4);

for(i4=0;i4120;i4++){

for(i=0;i5;i++)

aa[i].smoke=smoke[i];

rot(smoke,5);

for(i5=0;i5120;i5++){

for(j=0;j5;j++)

aa[j].pads=pads[j];

rot(pads,5);

if(OK()){

printf("---------------------第%d种解---------------------\n",++ans);

for(k=0;k5;k++)

printf("第%d户: %s人 %s房子 养%s 喝%s 抽%s\n",k+1,GUO[aa[k].guojia],

COL[aa[k].color],PAD[aa[k].pads],DRK[aa[k].drink],SMK[aa[k].smoke]);

}}}}}}

return 0;

}