1. 100匹马驮100担货,大马一匹驮3担,中马一匹驮2担,小马两匹驮一担。
(1)大、中、小马都必须有,而且每匹马必须按要求驮货,有哪些组合方式?
(2)不要求三类马都有,但要求每匹马必须按要求驮货,有哪些组合方式?
(3)用三重while循环求第1种组合的情况。
(4)用二重do-while循环求第1种组合的情况。
(5)用三重for循环求第2种组合的情况。
(6)要求打印所有的组合方式以及可能的组合数量。
(7)要求分别打印三种算法所费时间是多少,计算一种算法所占机时的程序提示:
#include <time.h>
#include <conio.h>
#include <dos.h>
main()
{ clock_t start, end; /* time_t start1, end1;*/
int i, big, middle, small, ncount;
clrscr();
start = clock(); /* start1 = time(NULL);*/
big = 1; middle = 1; small = 1;
ncount = 0;
printf("This a while program\n");
while (big <= 100)
{ . . . . . .
}
end = clock(); /* end1 = time(NULL);*/
printf("The num of method1 is: %d\n", ncount);
printf("and the time is: %5.1f time\n", (double)(end-start)/18.2);
/*printf f("The difference is :%5.1f second\n", difftime(end1, start1));*/
. . . . . .
}
关于clock()和time()函数,大家自己查阅相关资料。时间如果很短,可能会输出0.0,大家可以把100增大,比如1000匹马,1000担货,自己调试。
(1)大、中、小马都必须有,而且每匹马必须按要求驮货,有哪些组合方式?
(2)不要求三类马都有,但要求每匹马必须按要求驮货,有哪些组合方式?
(3)用三重while循环求第1种组合的情况。
(4)用二重do-while循环求第1种组合的情况。
(5)用三重for循环求第2种组合的情况。
(6)要求打印所有的组合方式以及可能的组合数量。
(7)要求分别打印三种算法所费时间是多少,计算一种算法所占机时的程序提示:
#include <time.h>
#include <conio.h>
#include <dos.h>
main()
{ clock_t start, end; /* time_t start1, end1;*/
int i, big, middle, small, ncount;
clrscr();
start = clock(); /* start1 = time(NULL);*/
big = 1; middle = 1; small = 1;
ncount = 0;
printf("This a while program\n");
while (big <= 100)
{ . . . . . .
}
end = clock(); /* end1 = time(NULL);*/
printf("The num of method1 is: %d\n", ncount);
printf("and the time is: %5.1f time\n", (double)(end-start)/18.2);
/*printf f("The difference is :%5.1f second\n", difftime(end1, start1));*/
. . . . . .
}
关于clock()和time()函数,大家自己查阅相关资料。时间如果很短,可能会输出0.0,大家可以把100增大,比如1000匹马,1000担货,自己调试。