public class jisuan { public static final int horse = 100; public static final int rice = 100; public static int xNum; public static int yNum; public static int zNum; public static int count = 0;
public class TestHorseAndGrain { static final int HORSE_QUANTITY = 100;//粮食总量 static final int GRAIN_QUANTITY = 100;//大马中马小马总和 static final int BIG_CAPACITY = 3;//每只大马的载重量 static final int MIDDLE_CAPACITY = 2;//每只中码的载重量 static final double LITTLE_CAPACITY = 0.5;//每只小马的载重量
public static void main(String[] args) { int big,middle,little;;//分别表示大马,中马,小马的数量 for(big = 1; big*BIG_CAPACITY<=GRAIN_QUANTITY;big++) { for(middle=1; middle*MIDDLE_CAPACITY<=(GRAIN_QUANTITY-big*BIG_CAPACITY);middle++) { little = HORSE_QUANTITY-big-middle; int grain=GRAIN_QUANTITY-big*3-middle*2; if(little>0&&little%MIDDLE_CAPACITY==0&&little*LITTLE_CAPACITY==grain) System.out.println(big + " " +middle + " " + little ); } } } }
刚学python。拿来试试手 x=range(0,100,3) y=range(0,100,2) z=range(0,200) for a in x: for b in y: for c in z: if(a*3+b*2+c)==100: print(a,b,c)
x=1,Y=48,z=51 x=4,Y=44,z=52 x=5,Y=42,z=53 x=8,Y=38,z=54 x=9,Y=36,z=55 x=12,Y=32,z=56 x=13,Y=30,z=57 x=16,Y=26,z=58 x=17,Y=24,z=59 x=20,Y=20,z=60 x=21,Y=18,z=61 x=24,Y=14,z=62 x=25,Y=12,z=63 x=28,Y=8,z=64 x=29,Y=6,z=65 x=32,Y=2,z=66var x,y,z:integer; begin for x:= 1 to 100 do begin for y:= 1 to 100 do begin for z:= 1 to 100 do begin if (x+y+z=100) and (3*x+2*y+(z mod 2)=100) then begin memo1.Lines.Add('x='+inttostr(x)+',Y='+inttostr(y)+',z='+inttostr(z)); end; end; end; end;end; 不知道对不对。
/** 171. * 100匹马背100担粮。 大马一匹背3担,中马一匹背2担。小马2匹背一担。请编程输出所有满足条件的情况 172. * 6i+4j+k = 200 173. * i+j+k = 100 174. * 相减得出 175. * 5i + 3j = 100 176. * k = 100 - i - j; 177. * 求所有 ijk的组合 178. */ 179. public static void horseTest() { 180. 181. for (int i = 0; i <= 20; i++) { 182. int j = (100 - i * 5) / 3; 183. int k = 100 - i - j; 184. 185. if (i * 3 + j * 2 + k * 0.5 == 100) 186. System.out.println("i:" + i + "\tj:" + j + "\tk:" + k); 187. } 188. 189. } 190.
x+y+z= 100
3x+2y+z/2=100;
极限 x=33,y=50,z =100
做个3层嵌套 ,满足条件的就是
for(Integer a=0;a<34;a++){
for(Integer b=0;b<(100-3*a)/2;b++){
for(Integer c=0;c<(100-3*a-2*b)*2;c++){
if((3*a+2*b+0.5*c)==100){
System.out.println("a="+a.toString()+",b="+b.toString()+",c="+c.toString());
break;
}
}
}
}
b<=(100-3*a)/2和c<=(100-3*a-2*b)*2;
if((3*a+2*b+0.5*c)==100 && 100 == a + b + c){
public static final int horse = 100;
public static final int rice = 100;
public static int xNum;
public static int yNum;
public static int zNum;
public static int count = 0;
public static void main(String[] args) {
for (xNum = 1; xNum < horse; xNum++) {
for (yNum = 1; yNum < horse; yNum++) {
for (zNum = 1; zNum < horse; zNum++) {
if (3 * xNum + 2 * yNum + 0.5 * zNum == rice && xNum + yNum + zNum == horse) {
count ++;
System.out.println("第" + count + "种答案。");
System.out.println("大的" + xNum + "," + "中的" + yNum + "," + "小的" + zNum);
System.out.println("-------------华丽的分割线-------------");
}}}}}}
int a0,a1,a2;
for(a0=0;a0<33;a0++){
for(a1=0;a1<50;a1++){
a2=200-6*a0-4*a1;
if(a2<0){
break;
}
System.out.println(a0+" "+a1+" "+a2);
}
}
还缺个条件 a2>100也要中断
{
for(int b=0;b*2<=100;b++)
{
for(int c=0;c<=100;c++)
{
if((a+b+c)==100&&(a*3+b*2+0.5*c)==100)
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println("--------");
}
}
}
}
static final int HORSE_QUANTITY = 100;//粮食总量
static final int GRAIN_QUANTITY = 100;//大马中马小马总和
static final int BIG_CAPACITY = 3;//每只大马的载重量
static final int MIDDLE_CAPACITY = 2;//每只中码的载重量
static final double LITTLE_CAPACITY = 0.5;//每只小马的载重量
public static void main(String[] args) {
int big,middle,little;;//分别表示大马,中马,小马的数量
for(big = 1; big*BIG_CAPACITY<=GRAIN_QUANTITY;big++) {
for(middle=1; middle*MIDDLE_CAPACITY<=(GRAIN_QUANTITY-big*BIG_CAPACITY);middle++) {
little = HORSE_QUANTITY-big-middle;
int grain=GRAIN_QUANTITY-big*3-middle*2;
if(little>0&&little%MIDDLE_CAPACITY==0&&little*LITTLE_CAPACITY==grain)
System.out.println(big + " " +middle + " " + little );
}
}
}
}
极限 x=33,y=50,z =100
循环第一层不能超过33(另两层如果作为第一层循环同理)不注意的很可能三层都是100循环。那样你只能得60分
如果注意了极限,那你至少90分。
{
int big, middle, little;
double total;
for (big = 0; big <= 33; big++)
{
for (middle = 0; middle <= 50; middle++)
{
little = 100 - big - middle;
total = 3 * big + 2 * middle + 0.5 * little;
if (Math.abs(total - 100) < 0.01)
{
System.out.println(big + " " + middle + " " + little);
}
}
}
}
public static void main(String[] args) {
for(int b=0;b<=getBigCount(100);b++){
for(int m=0;m<=(100-3*b)/2;m++){
for(int l=0;l<=(100-3*b-2*m)*2;l++){
if((b*3+m*2+l/2)==100 && (b+m+l)==100)
System.out.println("big horse "+b+" middle horse "+m +"little horse" +l);
}
}
}
}
public static int getBigCount(int total){
//b+l=100;
//3b+l/2=100;
int bmax=total/5;
return bmax;
}
}//为什么是33呢 33匹大马能背99包,剩下的一包必须由67匹马来背,不符合要求,看我的
{
int i;
int old, mid, yog;
for (i = 0; i <= 20; i++)
{
old = i;
mid = (100 - 5 * old) / 3;
if ( (mid * 3 + old * 5) != 100)
continue;
yog = 200 - 6 * old - 4 * mid;
if ((yog % 2) == 1)
continue;
printf("old = %-4d, mid = %-4d, young = %-4d, total = %-4d, grain = %-4d\n", old, mid, yog, old + mid + yog, 3 * old + 2 * mid + yog /2);
}
}
结果:
[code=C/C++]
old = 2 , mid = 30 , young = 68 , total = 100 , grain = 100
old = 5 , mid = 25 , young = 70 , total = 100 , grain = 100
old = 8 , mid = 20 , young = 72 , total = 100 , grain = 100
old = 11 , mid = 15 , young = 74 , total = 100 , grain = 100
old = 14 , mid = 10 , young = 76 , total = 100 , grain = 100
old = 17 , mid = 5 , young = 78 , total = 100 , grain = 100
old = 20 , mid = 0 , young = 80 , total = 100 , grain = 100
[/code]
5个大马+3个中马=100
的话 得知 大马最小是2 每次涨3 最大20
这样就相当简单了
//5*a0+3*a1=100;
int a0,a1,a2;
for(a0=2;a0<=20;a0+=3){
a1=(100-5*a0)/3;
a2=100-a0-a1;
System.out.println(a0+" "+a1+" "+a2);
}
//b+l=100;
//3b+l/2=100;
我说的"天马流星拳",只是为了渲染气氛而已(幽默一下).哈
依题意:设大马匹数为X(匹),中马y(匹),小马z(匹).那么可以列出方程
x+y+z=100
3x+2y+z/2=100
0<=x<=33
0<=y<=50
0<=z<=100并且z必须能被2整除(因为一只小马背不动一担粮.)
其中x,y,z都是整数.
解出x,y,z
以上的思路用java来实现,如下:class Horse
{
int x;//大马匹数
int y;//中马
int z;//小马
int i=0;//用于统计满足条件的个数
int count_horse;//马的总匹数
int count_foot;//粮食的总担数
public void Operation()
{
for(z=0;z<=100;z++)
{
for(y=0;y<=50;y++)
{
x=100-y-z;
if( (x>=0) && (x<=33) && (y>=0) && (y<=50) && (z>=0) && (z<=100) && (z%2==0) )
{
if( ((x+y+z)==100) && ((3*x+2*y+z/2)==100) )
{
i++;
System.out.println("第【"+i+"】种情况--大马:"+x+" 中马:"+y+" 小马:"+z);
//检测结果是否满足题意
count_horse=x+y+z;//马的匹数
count_foot=3*x+2*y+z/2;//粮食的总担数
System.out.println("[检测]马的总匹数:"+count_horse+" 粮食的总担数:"+count_foot+"\n");
}
}
}
}
}
public static void main(String[]args)
{
Horse h=new Horse();
h.Operation();
}
}打印结果:第【1】种情况--大马:2 中马:30 小马:68
[检测]马的总匹数:100 粮食的总担数:100第【2】种情况--大马:5 中马:25 小马:70
[检测]马的总匹数:100 粮食的总担数:100第【3】种情况--大马:8 中马:20 小马:72
[检测]马的总匹数:100 粮食的总担数:100第【4】种情况--大马:11 中马:15 小马:74
[检测]马的总匹数:100 粮食的总担数:100第【5】种情况--大马:14 中马:10 小马:76
[检测]马的总匹数:100 粮食的总担数:100第【6】种情况--大马:17 中马:5 小马:78
[检测]马的总匹数:100 粮食的总担数:100第【7】种情况--大马:20 中马:0 小马:80
[检测]马的总匹数:100 粮食的总担数:100Java编程技术交流QQ群:171396965 技术需要交流,技术需要与时俱进.这里是优秀爱好Java编程的集聚地,我们一起学习,共同进步.加入时请注明Java字样,谢谢. 博客:http://blog.csdn.net/StellaAh/article/category/866574
x+y+z = 100 (1)
3x+2y+z/2 = 100 (2)
上述两式联立并消去z可得:
5x+3y = 100 (3)
当x = 21时,大马背运货物为3x = 63
这种情况下即使中马y = 0,小马为79,则小马背运货物为z/2 = 39
此时3x + 2y + z/2 = 102 > 100,因此大马数量x必须小于等于20
以下为代码:public class PortageCompute { public PortageCompute() {
super();
} public static void main(String[] args) {
int smallHorse = 0;
int midHorse = 0;
int result_count = 0;
for (int bigHorse = 0; bigHorse <= 20; bigHorse++) {
/* 根据式(3) */
if ((100 - 5 * bigHorse) % 3 != 0) {
continue;
} else {
result_count++;
midHorse = (100 - 5 * bigHorse) / 3;
smallHorse = 100 - bigHorse - midHorse;
System.out.println("第" + result_count + "种算法: 大马[" + bigHorse +
"]匹,中马[" + midHorse + "]匹,小马[" +
smallHorse + "]匹。");
}
}
}
}
for 循环嵌套,很多初级编程 的考试题...同理百钱买百鸡
x=range(0,100,3)
y=range(0,100,2)
z=range(0,200)
for a in x:
for b in y:
for c in z:
if(a*3+b*2+c)==100:
print(a,b,c)
x=4,Y=44,z=52
x=5,Y=42,z=53
x=8,Y=38,z=54
x=9,Y=36,z=55
x=12,Y=32,z=56
x=13,Y=30,z=57
x=16,Y=26,z=58
x=17,Y=24,z=59
x=20,Y=20,z=60
x=21,Y=18,z=61
x=24,Y=14,z=62
x=25,Y=12,z=63
x=28,Y=8,z=64
x=29,Y=6,z=65
x=32,Y=2,z=66var
x,y,z:integer;
begin
for x:= 1 to 100 do
begin
for y:= 1 to 100 do
begin
for z:= 1 to 100 do
begin
if (x+y+z=100) and (3*x+2*y+(z mod 2)=100) then
begin
memo1.Lines.Add('x='+inttostr(x)+',Y='+inttostr(y)+',z='+inttostr(z));
end; end;
end;
end;end;
不知道对不对。
171. * 100匹马背100担粮。 大马一匹背3担,中马一匹背2担。小马2匹背一担。请编程输出所有满足条件的情况
172. * 6i+4j+k = 200
173. * i+j+k = 100
174. * 相减得出
175. * 5i + 3j = 100
176. * k = 100 - i - j;
177. * 求所有 ijk的组合
178. */
179. public static void horseTest() {
180.
181. for (int i = 0; i <= 20; i++) {
182. int j = (100 - i * 5) / 3;
183. int k = 100 - i - j;
184.
185. if (i * 3 + j * 2 + k * 0.5 == 100)
186. System.out.println("i:" + i + "\tj:" + j + "\tk:" + k);
187. }
188.
189. }
190.
每+1大马 相当于多加了2份粮食
中马 相当于加了 1份粮食
两匹小马 相当于消耗了一匹马每加一匹大马必须 由 4匹小马来中和才相当于 5匹马5份粮食 100以内最大可以为20种可能
一匹中马 需要两匹小马中和 3匹马3份所以必须能整除 5 和 3
而且 三匹马 必须和为100
for(int i = 0;i<=20;i++)
{
int mid = (100 - 5i ) / 3 ;
int yong = 100-i-mid;
if(6*i+4*mid+yong == 200) // 都扩大2倍
{
System.out.println(i+","+mid+","+yong);
}
}
System.out.println("a="+a.toString()+",b="+b.toString()+",c="+c.toString());
break;
中的break吗?
不知说的对不对,冒昧了,请原谅!
这句话有一点不明白啊,能不能帮忙解释一下啊?