日期:2014-05-20 浏览次数:20738 次
public class Test3 { public static void main(String[] args) { int men; int women; int kids; for (men=0; men <= 50 / 3; men++) { for (women=0; women <= 50 / 2; women++) { for (kids=0; kids <= 50; kids++) { if ((3 * men + 2 * women + kids == 50) && (men + women + kids == 30)) { System.out.println("男人:" + men + ", 女人:" + women + ", 小孩:" + kids); } } } } } }
------解决方案--------------------
package test;
public class Test {
public static void main(String[] args) {
int x; // 男人
int y; // 女人
//小孩, 因为 x+y+z = 30 所以 z = 30-x-y
System.out.println("男人\t女人\t小孩(30-x-y)");
for (x = 0; x <= 50/3; x++) { //男人数不会超过50 /3 个
for (y = 0; y <= 50/2; y++) { //女数不会超过50 /2 个
if (3 * x + 2 * y + (30 - y - x) == 50) {
System.out.println(x +"\t" + y + "\t"
+ (30 - x - y));
}
}
}
}
}
------解决方案--------------------
唉,好笨,自己想不出来,只好看别人的了。
------解决方案--------------------
其实women和kids的范围可以再缩小一下
0《men《5/3
0《women《(50-men*3)/2
0《kids《50-men*3-women*2
------解决方案--------------------
这个问题很有趣,刚看到题目我还想写呢!但是1楼已经的非常好了,我就懒得写了。
------解决方案--------------------
public class Test02 {
public static void main(String[] args) {
int men ,women,kids;
for(men =0;men<=10;men++){
for (women =0; women <= 30; women++) {
for (kids =0; kids <= 30; kids++) {
if((men*3+women*2+kids==50) && (men+women+kids==30)){
System.out.println("men有 "+men+" women有"+women+" kids有"+kids);
}
}
}
}
}
}
------解决方案--------------------
731564363