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);*/
}
程序分析 多层嵌套从上往下阅读,阅读到最内层嵌套时阅读循环,循环直至跳出(记得要在外层将内层变量重新赋值初始值因为内层循环已经将内层变量的值循环到跳出值)