原题:在马克思手稿中有一道趣味的数学问题:一共有30个人,可能包括男人,女人和小孩。他们在一家饭馆吃饭共花了50先令,其中每个男人花了3先令,每个女人花2先令,每个小孩花1先令。请问:女人和男人,小孩分别是几个人,请编写一个程序来计算。利用for循环实现提示:定义三个变量men , women 和 kids 分别表示男人数,女人数和小孩数,可以得到如下两个关系:men+ women+kids = 30 ; 3*men+2*women+kids=50男人数i的范围是0<=men<=10,利用for循环实现.========================================================我的思路:让men,women进入循环,将kids直接嵌入表达式,使其的值为:总人数-(men+women),在分数的循环上,因kids不用乘以任何数,因此也符合其不用进入循环。【这个思路是错的,因为写出来的代码根本不对】我的代码:/*
 * 错误待纠正
 */
public class P142_4{
public static void main (String[]args){
int men =0 ;
int women=0;
int kids=0;
int a = women+kids;
for ( int i = 1 ;i <=10 ;i++,men++,kids++ ){
for ( women = 1; women <=30-(men+kids);women++){
int money = men*3 + women*2 +kids ;
System.out.println("****************");
System.out.println("男人有:"+men);
System.out.println("女人有:"+women);
System.out.println("小孩有:"+kids);
System.out.println("****************");
System.out.println("分数是:"+money);
int people = men + a ;
System.out.println("****************");
System.out.println("人数是:"+people);
if ( people == 30 && money==50 ){
System.out.println("男人有:"+men);
System.out.println("女人有:"+women);
System.out.println("小孩有:"+kids);
    break;}
}
}
System.out.println("程序结束!");
}
}求高手给思路,顺带给代码谢谢了。

解决方案 »

  1.   

    顺便问一句:查java的api是什么意思?
      

  2.   

    API(Application Programming Interface),即应用程序编程接口,是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。
      

  3.   

    提示都有了
    3层嵌套for循环
      

  4.   


    int man,woman,kid,jiFen;
    for (int i = 1; i <30; i++) {
    man = i;
    for (int j = 1; j < 30; j++) {
    woman = j;
    kid = (30-man-woman);
    jiFen = man*3+woman*2+kid;
    if(jiFen==50){
    System.out.println(man+"*3 +"+woman+"*2+"+kid+"=50");
    }
    }
    }
      

  5.   

    public class Test21 { public static void main(String[] args) { for (int man = 0; man < 17; man++) {
    for (int woman = 0; woman < 26; woman++) {
    for (int child = 0; child < 50; child++) {
    if (man + woman + child == 30
    && man * 3 + 2 * woman + child == 50) {
    System.out.println("男人有:" + man);
    System.out.println("女人有:" + woman);
    System.out.println("小孩有:" + child);
    System.out.println("****************");
    }
    }
    }
    } System.out.println("程序结束!");
    }
    }
      

  6.   

    一般不是说只有两层嵌套for循环的吗?求源代码,谢谢。
      

  7.   

    两层就够了。
    for ( int i = 0 ;i <=10;i++){
     for(int j= 0;j<=30-3*i;j++){
         int count = 3*i+2*j+(30-i-j);
         if(count==50){
    System.out.println(i+" "+j+" "+(30-i-j));

    }
    }
      

  8.   

    public class Test21 { public static void main(String[] args) { for (int man = 0; man < 17; man++) {
    for (int woman = 0; woman < 26; woman++) {
    if (man * 3 + 2 * woman + (30 - man - woman) == 50) {
    System.out.println("男人有:" + man);
    System.out.println("女人有:" + woman);
    System.out.println("小孩有:" + (30 - man - woman));
    System.out.println("****************");

    }
    }
    } System.out.println("程序结束!");
    }
    }这是两层循环
      

  9.   

    谢谢,我按照您的思路,写出了自己的代码。
    如下:
    /*
     * 思路:使用两层嵌套for循环
     * 将i的值设为小于总人数30,并且将男人的数量与i相等
     * 同理,j的值等于女人的数量。
     * 用运算公式表达小孩的数量等于30减男人再减女人(注意括号的使用!)
     * 使用if判断总分数是否等于50,若等于50输出正确,否则输出错误!
     */
    public class P142_4{
    public static void main (String[]args){
    int men,women,kids,money ;//变量一次是:男人,女人,小孩,花的先令之和
    for ( int i = 1 ;i <30 ;i++ ){ //定义i的值小于总人数30
    men = i ;//定义男人的数量等于i
    for ( int j  = 1; j <30;j++){//定义j的值小于总人数30
    women = j;//定义女人的数量等于j
    kids = (30- men - women ); //这步很重要!!!定义小孩的数量,注意括号的使用!!
      money = men*3 + women*2 +kids ;//计算花费的先令总数
    if ( money == 50 ){//当花费的先令总数等于50时,输出正确的数值
    System.out.println("****************");
    System.out.println("男人的数量是:"+men+"\t女人的数量是:"+women+"\t小孩的数量是:"+kids);
    System.out.println("先令总额"+"3*"+men+"+2*"+women+"+"+kids+"=50");
    System.out.println("总人数:");
    System.out.println(men+women+kids);
    }
    }
    }
       System.out.println("程序结束!");
    }
    }
      

  10.   

    典型的“百鸡问题”
    public class P142_4 {    public static void main(String[] args) {
         int i,j,k;
         System.out.println("  i  j  k");
         for(i=1;i<=16;i++) //i是男人数
         {
         for(j=0;j<=25;j++) //j是女人数
         {
         k=30-i-j;    //k是小孩数
         if(3*i+2*j+i==50)
         System.out.println("  "+i+"  "+j+"  "+k);
         }
         }
        }
        
        
    }