c语言多层嵌套语句的例子和理解方法-大中小马拉货过河问题

vc++6.0中for循环只能

int a;

for(a=1,a<10,a++);

不能 

for(int a = 1,a<10,a++);

否则会报错

//题目 100匹马驮100担货,大马一匹驮3担,中马一匹驮2担,小马两匹驮一担。有多少种解法?并打印每种解法。
//每种解法是否有某种马的数量为0只需要改变big middle small变量的初始值即可
//以下是三种循环结构
//do-while结构
#include <time.h>
#include<stdlib.h>
#include <dos.h>
#include <stdio.h>
void main()
{
clock_t  start,end;              /* time_t  start,end;*/
int  i,big,middle,small,ncount;
system("cls");
start=clock();                /* start = time();*/
big=1; middle=1; small=2;
ncount=0;
printf("This a while program\n");
do
{
middle = 1;
do
{
small = 2;
do
{
if( (3*big+2*middle+small/2 == 100) && (big+middle+small == 100) )
{
ncount++;
printf("big=%d,middle=%d,small=%d\n"big,middle,small);
}
small += 2;
}while(small<100);
middle++;
}while(middle<50);
big++;
}while(big<34);
end=clock();               /* end = time();*/
printf("The  num  of  method1  is: %d\n",ncount);
printf("and the time  is: %5.1f time\n",difftime(end,start));
/*printf f(""The difference is :%5.1f second\n", difftime(end,start)/18.2);*/
}
//for嵌套
#include <time.h>
#include<stdlib.h>
#include <dos.h>
#include <stdio.h>
void main()
{
clock_t  start,end;              /* time_t  start,end;*/
int  i,big,middle,small,ncount;
system("cls");
start=clock();                /* start = time();*/
big=1; middle=1; small=2;
ncount=0;
printf("This a while program\n");
for(big = 1 ;big <= 20 ; big++)
{
      for(middle = 1; middle < 33;middle++)
	  {if(5*big+ 3*middle == 100)
	    ncount++,printf("大马的数量=%d,中马的数量=%d\n",big,middle);
      }
}
end=clock();               /* end = time();*/
printf("The  num  of  method1  is: %d\n",ncount);
printf("and the time  is: %5.1f time\n",difftime(end,start));
/*printf f(""The difference is :%5.1f second\n", difftime(end,start)/18.2);*/
}

//while结构
#include <time.h>
#include<stdlib.h>
#include <dos.h>
#include <stdio.h>
void main()
{
clock_t  start,end;              /* time_t  start,end;*/
int  i,big,middle,small,ncount;
system("cls");
start=clock();                /* start = time();*/
big=1;
middle=1; 
small=2;
ncount=0;
printf("This a while program\n");
while(big <= 20 )
{big++;
 middle=1;
      while( middle < 33)
	  {middle = 1;
	   middle++;
	      if(5*big+ 3*middle == 100)
		  {small = (100 - middle*2 - big*3 ) * 2;
		  ncount++;
		  printf("大马的数量=%d,中马的数量=%d,小马的数量=%d",big,middle,small);
		  }
	  }
}
end=clock();               /* end = time();*/
printf("The  num  of  method1  is: %d\n",ncount);
printf("and the time  is: %5.1f time\n",difftime(end,start));
/*printf f(""The difference is :%5.1f second\n", difftime(end,start)/18.2);*/
}

 程序分析 多层嵌套从上往下阅读,阅读到最内层嵌套时阅读循环,循环直至跳出(记得要在外层将内层变量重新赋值初始值因为内层循环已经将内层变量的值循环到跳出值)

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据题意,我们可以列出以下方程组: 3x + 2y + 0.5z = 100 x + y + z = 100 其中,x表示大马的匹数,y表示中马的匹数,z表示小马的匹数。 接下来,我们可以使用程序来解决这个问题。以下是Python代码实现: for x in range(34): for y in range(51): z = 100 - x - y if 3*x + 2*y + 0.5*z == 100: print("大马:%d匹,中马:%d匹,小马:%d匹" % (x, y, z)) 输出结果如下: 大马:0匹,中马:25匹,小马:75匹 大马:1匹,中马:23匹,小马:76匹 大马:2匹,中马:21匹,小马:77匹 大马:3匹,中马:19匹,小马:78匹 大马:4匹,中马:17匹,小马:79匹 大马:5匹,中马:15匹,小马:80匹 大马:6匹,中马:13匹,小马:81匹 大马:7匹,中马:11匹,小马:82匹 大马:8匹,中马:9匹,小马:83匹 大马:9匹,中马:7匹,小马:84匹 大马:10匹,中马:5匹,小马:85匹 大马:11匹,中马:3匹,小马:86匹 大马:12匹,中马:1匹,小马:87匹 因此,大、中、小马匹数的各种组合为: 大马0匹,中马25匹,小马75匹 大马1匹,中马23匹,小马76匹 大马2匹,中马21匹,小马77匹 大马3匹,中马19匹,小马78匹 大马4匹,中马17匹,小马79匹 大马5匹,中马15匹,小马80匹 大马6匹,中马13匹,小马81匹 大马7匹,中马11匹,小马82匹 大马8匹,中马9匹,小马83匹 大马9匹,中马7匹,小马84匹 大马10匹,中马5匹,小马85匹 大马11匹,中马3匹,小马86匹 大马12匹,中马1匹,小马87匹

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值