风天邪性
2014-12-28 09:01
采纳率: 66.7%
浏览 1.9k
已采纳

JAVA循环题,,求大神指教

100元怎么买100个蛋,鸡蛋1毛一个,鸭蛋3元一个,鹅蛋6元一个

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • 不得了啦 2014-12-28 09:47
    已采纳
     public class Egg{
        public static void main(String[] args){
            int chicken=0,duck=0,goose=0;
            for(int i=0;i<100;i++){
                chicken = i;
                    for(int j=0;j<100;j++){
                        duck = j;
                        goose = 100-duck-chicken;
                        if(chicken+duck*30+goose*60==1000&&chicken>0&&duck>0&&goose>0){ 
                            System.out.println("鸡:"+chicken+"  鸭:"+duck+"  鹅:"+goose);
                        }                   
                    }
            }               
        }   
    }
    
    打赏 评论
  • 蒲锦_up 2014-12-28 09:49

    这不是java问题 是数学问题

    打赏 评论
  • 0808xyj 2014-12-29 03:00
     public class HowToBuyEgg {
        public static int getNumberOfEggs(int eggNum,int money,int pChikchen,int pDuck, int pGoose){
            int gooseNum = money/pGoose;
            int duckNum = money/pDuck;
            for(int i = 0; i <= gooseNum;i++){
                for(int j = 0; j <= duckNum;j++){
                    if((pGoose*i+pDuck*j+(eggNum-i-j)*pChikchen*0.1) == money){
                        System.out.println("All "+money+" RMB can buy "+(100-i-j)+" eggs and " + j + " duck eggs and "+ i + " goose eggs");
                    }
                }
            }
            return 0;
        }
    
        public static void main(String args[]){
            getNumberOfEggs(100,100,1,3,6);
        }
    }
    

    All 100 RMB can buy 70 eggs and 29 duck eggs and 1 goose eggs

    打赏 评论
  • saintheal 2021-09-04 19:29

    img

    打赏 评论
  • 吉普赛的歌 2014-12-28 10:57
     //x+y+z=100
    //0.1x+3y+6z=100
    
    for(int x=1;x<=100;x++){
        for(int y=1;y<=100;y++){
            for(int z=1;z<=100;z++){
                if( (x+y+z=100) && (0.1x+3y+6z=100) ){
                    System.out.println("鸡:"+x+"  鸭:"+y+"  鹅:"+z);
                }
            }
        }
    }
    
    1 打赏 评论

相关推荐 更多相似问题