百马百担问题用C语言怎么编啊?各位高手 请求帮助啊 谢谢了

目标:100匹马驮100担货;
假设:大马一匹驮3担,中马一匹驮2担,小马两匹驮一担;
组合方法1:大马、中马、小马每种不能少于一匹;
组合方法2:对马匹种类无限制,可以缺少一种或缺二种
问题: 1. 采用组合方法1,用while求解,输出所有组合法,输出组合的总数?
2.采用2种组合,用do-while求解,输出所有组合法,输出组合的总数?
3.采用2种组合,用三重或二重for循环求解,输出所有组合法,输出组合的总数?
4.除打印结果和多少种组合法外,还要分别打印三种算法所费机时。
提示:计算一种算法所占机时的程序提示:
#include <time.h>
#include <conio.h>
#include <dos.h>
main()
{
clock_t start,end; /* time_t start,end;*/
int i,big,middle,small,ncount;
clrscr();
start=clock(); /* start = time();*/
big=1; middle=1; small=2;
ncount=0;
printf("This a while program\n");
while (big<=33)
{
.
}
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);*/
……}
各位好好人 帮帮忙啊 我后天等着急用呢 谢谢了

第1个回答  2012-03-28
先记下来,白天我帮你想想这个问题吧!貌似以前做过这个的。。。。追问

额 好 谢谢啊 不过 我周四晚上就要了 所以 麻烦了你 谢谢谢谢

追答

话说我也有作业的,所以不能所有的都帮你做了,现在给第一个的答案给你吧!
1. 采用组合方法1,用while求解,输出所有组合法,输出组合的总数?

#include
void print(int s[])
{

printf("%d %d %d",s[0],s[1],s[2]);
printf("\n");
}
void main()
{
printf("大马 中马 小马\n");
int s[3];
s[0]=100/3;
while(s[0]>=1)
{
s[1]=(100-3*s[0])/2;
while(s[1]>=1)
{
s[2]=100-3*s[0]-2*s[1];
print(s);
s[1]--;
}
s[0]--;
}
}

相似回答