2 qq 37227163 qq_37227163 于 2017.01.01 22:36 提问

求大神帮我看看这道题@-@为什么运行后无法结束?????

马克思手稿中有这样一道趣味数学题:男人、女人和小孩总计n个人,在一家饭店里吃饭,共花了cost先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,请用穷举法编程计算男人、女人和小孩各有几个。

#include
int main()
{
int i,j,k,n,m,answer;
scanf("%d%d%d",i,j,k);
answer=0;
for(i=0;i<=n/3;i++)
{
for(j=0;j<=n/2;j++)
{
k=m-j-k;
}
if(i*3+j*2+k==n)
{
printf("%d%d%d",i,j,k);
answer==1;
}
if(answer==0)
printf("No answer");

}return 0;

}
为什么运行程序后无法结束??????

3个回答

caozhy
caozhy   Ds   Rxr 2017.01.01 23:46
 answer==1;
->
answer=1;
ai977313677
ai977313677   2017.01.01 23:48

首先,没看到你对m的赋值操作在哪里,其次,if里面不要写一串算式而且又不加括号,你对运算符的优先级和结合方式不了解,很容易出错,但建议不要像你这样写,再者,你的answer中有一个判等,那是干什么?判等一般是出现在分支,循环等语句中,出现在这里没有什么意义,赋值是一个等号,判等是两个等号。

xiangzyuan
xiangzyuan   2017.01.01 23:51

you sure that you can compile and run this program???
1.no header file for include
2.wrong using for scanf function, it should be scanf("%d", &x)
3.the count of female will be over limit. You may get -1 children
4.....nerver mind.
#include
int main(int argc, char const *argv[])
{
int cost, count, count_male, count_female, count_children;
int answers = 0;
scanf("%d %d", &cost, &count);
for(count_male=0; count_male<=count; count_male++)
for(count_female=0; count_female<=(count-count_male); count_female++)
{
count_children = count - count_male - count_female;
if((count_children + count_female*2 + count_male*3) == cost)
{
answers ++;
printf("male:%d, female:%d, chiledn:%d\n", count_male, count_female, count_children);
}
}

printf("total answers: %d\n", answers);
return 0;

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!