本帖最后由 a747631302 于 2012-04-18 10:49:28 编辑

解决方案 »

  1.   


    package com.test;public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    for(int a=0;a<20;a++){
     for(int b=0;b<33;b++){
     if((a*5+b*3+(100-(a+b))/3)==100&&(100-(a+b))%3==0){
     System.out.println("公鸡:"+a+"母鸡:"+b+"小鸡:"+(100-(a+b)));
     
     }
     }
    } }}
    公鸡:0母鸡:25小鸡:75
    公鸡:4母鸡:18小鸡:78
    公鸡:8母鸡:11小鸡:81
    公鸡:12母鸡:4小鸡:84
      

  2.   


    package com.test;import java.util.Scanner;public class Test2 { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int blance=1000;
    Scanner scan=new Scanner(System.in);
    while(true){
    System.out.println("取多少?");
    int x=scan.nextInt();
    if(x%100==0&&x<=blance){
    blance=blance-x;
    System.out.println("取出:"+x+"还剩:"+blance);
    }else if(x>blance){
    System.out.println("没钱了");
    break;
    } }}
    }
    取多少?
    900
    取出:900还剩:100
    取多少?
    900
    没钱了
      

  3.   

    百钱百鸡解法:设买公、母、小鸡个数分别为x,y,z,由题目得方程组:
    5*x+3*y+z/3=100
    x+y+z=100
    代入消z:
    15*x+9*y+(100-x-y)=300
    =>
    7*x+4*y=100
    因7与4互质且100、4*y都是4的倍数易知x为4的倍数,设x=4*i
    由于4*y>=0,得到:
    28*i<=100
    之后程序就可以写出来了: public static void main(String args[]) {
    for(int i=0;i<4;i++) {  // 由于100/28≈3.57,即i可取0,1,2,3,所以这里直接写循环条件为i<4
    int x=4*i;
    int y=(100-7*x)/4;
    int z=100-x-y;

    System.out.printf("公鸡:%4d,母鸡:%4d,小鸡:%4d\n",x,y,z);
    }
    }计算结果:
    公鸡:   0,母鸡:  25,小鸡:  75
    公鸡:   4,母鸡:  18,小鸡:  78
    公鸡:   8,母鸡:  11,小鸡:  81
    公鸡:  12,母鸡:   4,小鸡:  84
      

  4.   

    百钱百鸡的原题还有一个需求:公鸡、母鸡、小鸡数都不能为0。只需把x、y、z中有0的解抛掉即可: public static void main(String args[]) {
    for(int i=0;i<4;i++) {  // 由于100/28≈3.57,即i可取0,1,2,3,所以这里直接写循环条件为i<4
    int x=4*i;
    int y=(100-7*x)/4;
    int z=100-x-y;

    if(x>0&&y>0&&z>0)
    System.out.printf("公鸡:%4d,母鸡:%4d,小鸡:%4d\n",x,y,z);
    }
    }计算结果:
    公鸡:   4,母鸡:  18,小鸡:  78
    公鸡:   8,母鸡:  11,小鸡:  81
    公鸡:  12,母鸡:   4,小鸡:  84