给,已经编译运行确认:
1. 当大马,中马,小马每种不能少于1匹,有多少种组合法。
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
int count=0;
i=1;
j=1;
k=2;
while(i<100)
{
while(j<100)
{
while(k<100)
{
if((i+j+k==100)&&(3*i+2*j+k/2==100))
{
printf("大马:%d 中马:%d 小马:%d\n",i,j,k);
count++;
}
k=k+2;
}
k=2;
j++;
}
j=1;
i++;
}
printf("一共%d种组合法\n",count);
getch();
return 1;
}
运行结果为:
大马:2 中马:30 小马:68
大马:5 中马:25 小马:70
大马:8 中马:20 小马:72
大马:11 中马:15 小马:74
大马:14 中马:10 小马:76
大马:17 中马:5 小马:78
一共6种组合法
2. 当可以缺少一种或两种,又有多少种组合法。
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
int count=0;
i=0;
j=0;
k=0;
while(i<100)
{
while(j<100)
{
while(k<100)
{
if((i+j+k==100)&&(3*i+2*j+k/2==100))
{
printf("大马:%d 中马:%d 小马:%d\n",i,j,k);
count++;
}
k=k+2;
}
k=0;
j++;
}
j=0;
i++;
}
printf("一共%d种组合法\n",count);
getch();
return 1;
}
运行结果为:
大马:2 中马:30 小马:68
大马:5 中马:25 小马:70
大马:8 中马:20 小马:72
大马:11 中马:15 小马:74
大马:14 中马:10 小马:76
大马:17 中马:5 小马:78
大马:20 中马:0 小马:80
一共7种组合法
=====================================
如果不用while do...while语句该怎么写这程序?
=====================================
大哥,你早说啊,不用while更简单:
1. 当大马,中马,小马每种不能少于1匹,有多少种组合法。
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,count=0;
for(i=1;i<100;i++)//大马
for(j=1;j<100;j++)//中马
for(k=2;k<100;k=k+2)//小马 步长为2,因为小马不能为奇数,否则将有一个小马驮半担货
if((i+j+k==100)&&(3*i+2*j+k/2==100))
{
printf("大马:%d 中马:%d 小马:%d\n",i,j,k);
count++;
}
printf("一共%d种组合法\n",count);
getch();
return 1;
}
2. 当可以缺少一种或两种,又有多少种组合法。
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,count=0;
for(i=0;i<100;i++)//大马
for(j=0;j<100;j++)//中马
for(k=0;k<100;k=k+2)//小马 步长为2,因为小马不能为奇数,否则将有一个小马驮半担货
if((i+j+k==100)&&(3*i+2*j+k/2==100))
{
printf("大马:%d 中马:%d 小马:%d\n",i,j,k);
count++;
}
printf("一共%d种组合法\n",count);
getch();
return 1;
}
温馨提示:答案为网友推荐,仅供参考