#include
/*5户共井问题。有A,B,C,D,E 5家人共有一口井,已知井深不超过10m。
A,B,C,D,E 5家人的井绳长各不相同,从井口放下绳索正好到达水面时,
(a) 需要A家的绳2条接上B家的绳1条;
(b) 需要B家的绳3条接上C家的绳1条;
(c) 需要C家的绳4条接上D家的绳1条;
(d) 需要D家的绳5条接上E家的绳1条;
(e) 需要E家的绳6条接上A家的绳1条。 问井深和各家绳长。*/
int main()
{
int a,b,c,d,e,L; //设A,B,C,D,E的绳长分别为a,b,c,d,e,井深为L(不超过10m)。10m=1000cm
//2*a+b=L; 3*b+c=L; 4*c+d=L; 5*d+e=L; 6*e+a=L; 绳长与井深之间的关系式
L=500;
do
{ a=(L*265)/721;b=L-2*a;c=L-3*b;d=L-4*c;e=L-5*d;
if((L-6*e)<a) L=L-1;
else L=L+1;
} while ((L-6*e)!=a);
printf("a=%dcm b=%dcm c=%dcm d=%dcm e=%dcm L=%dcm\n",a,b,c,d,e,L);
return 0;
}
唉,无奈,学C没人解答
循环枚举井深和a长度
#include "stdio.h"
int main()
{
int a,b,c,d,e,L;
int flag = 0;
for (L=1;L<=1000;L++)
{
for (a=1;a<=L;a++)
{
b=L-2*a;
c=L-3*b;
d=L-4*c;
e=L-5*d;
if (6*e+a == L)
{
flag = 1;
break;
}
}
if (flag) break;
}
if (flag) printf("%d %d %d %d %d %d\n",a,b,c,d,e,L);
return 0;
}
你的思路不能保证正确,你可以一步一步调试看进入怎样的死循环了。