在马克思手稿中有一道数学去趣题:一共有30个人,可能包括男人,女人,小孩。他们在一家饭馆吃饭共花了
50先令,其中每个男人花3先令,每个女人花2先令,每个小孩花1先令,请问:男,女,小孩?
定义变量:men:男人;women:女人;kids:小孩

解决方案 »

  1.   

    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);
    }
    }
    }
    }
    }
    }
      

  2.   

     一到很简单枚举题啊。  可以直接枚举出来啊。
        men/3 男人不会超过的数量。   wonmen/2 同理      3 * men + 2 * women + kids == 50  men个男人 + wowen个女 + kids个小号  等于50  即这么多工花了50    men+wowen+kid = 30   即共有30 个能
      

  3.   


    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));}
    }
    }
    }
    }
      

  4.   

    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);
    }
    }
    }
    }
    }
    }