A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© jiajun 中级黑马   /  2016-8-5 18:48  /  5056 人查看  /  16 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

5黑马币
一瓶啤酒2块钱,2个空瓶可以换一瓶啤酒,4个盖子可以换一瓶啤酒,问给定一个资金,可以喝多少瓶啤酒,
算法怎么写

最佳答案

查看完整内容

public class BuyBeers { public static void main(String[] args) { System.out.println(buyBeers(16,0,0)); } public static int buyBeers(int money,int bottle,int cap) { //啤酒数量 int beerNum = money / 2 + bottle / 2 + cap / 4; //瓶子数量(本次新买 + 不能换的余下的) int bottleNum = beerNum + bottle % 2; //瓶盖数量(本次新买 + 不能换的余下的) int capNum = beerNum + cap % 4; // ...

16 个回复

倒序浏览
public class BuyBeers {

        public static void main(String[] args) {
                System.out.println(buyBeers(16,0,0));

        }
       
        public static int buyBeers(int money,int bottle,int cap) {
                //啤酒数量
                int beerNum = money / 2 + bottle / 2 + cap / 4;
                //瓶子数量(本次新买 + 不能换的余下的)
                int bottleNum = beerNum + bottle % 2;
                //瓶盖数量(本次新买 + 不能换的余下的)
                int capNum = beerNum + cap % 4;
                //总啤酒数量
                int beerSum = beerNum;
                //有2个以上瓶子或4个以上盖子
                if(bottleNum >= 2 || capNum >= 4) {
                        beerSum += buyBeers(0,bottleNum,capNum);
                }
                return beerSum;
        }
}

点评

你可以重载一个方法buyBeers(int money) {}只接受钱参数,然后方法内调用buyBeers(money,0,0);  发表于 2016-8-6 16:36
回复 使用道具 举报
回复 使用道具 举报
我给你看看哈
回复 使用道具 举报
我给你看看哈public class demo10 {         public static void main(String[] args) {                 int n=7;//人数                 System.out.println(n+"人共需要买:"+getCount(n)+"瓶");         }         public static int getCount(int n){                 int sum = 0;//用于记录当前啤酒总数                 int sum2 = 0;//用于记录当前啤酒总数                 int count = 0;//用于记录购买的啤酒数                                  while(sum<n){                         count++;                         sum++;                         if(sum%4==0 || sum%2==0){                                 sum++;                         }                 }                 return count;         } }
回复 使用道具 举报
public class demo10 {
        public static void main(String[] args) {
                int n=7;//人数
                System.out.println(n+"人共需要买:"+getCount(n)+"瓶");
        }
        public static int getCount(int n){
                int sum = 0;//用于记录当前啤酒总数
                int sum2 = 0;//用于记录当前啤酒总数
                int count = 0;//用于记录购买的啤酒数
               
                while(sum<n){
                        count++;
                        sum++;
                        if(sum%4==0 || sum%2==0){
                                sum++;
                        }
                }
                return count;
        }
}
回复 使用道具 举报
递归 思路 辛辛苦苦半天敲的都是 新手,求给我 ,多思考,多敲,多学习我们一起进步:
import java.util.Scanner;


public class Demo_Count {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int x  = sc.nextInt();
                int sum = count(x);
                System.out.println("一共能换"+sum+"瓶");
        }
//        思路:先用<4块钱>买2瓶酒(剩2个瓶子,2个盖子),
//        用(2个瓶子)换1瓶酒(剩1个瓶子,3个盖子);
//        再用<2块钱>买1瓶酒(剩2个瓶子,4个盖子),
//        用(2个瓶子,4个盖子)换2瓶酒(剩2个瓶子,2个盖子),
//        用(2个盖子)换1瓶酒(剩1个瓶子,3个盖子);
//        再用<2块钱>买1瓶酒(剩2个瓶子,4个盖子),
//        用(2个瓶子,4个盖子)换2瓶酒(剩2个瓶子,2个盖子),
//        用(2个瓶子)换1瓶酒(剩1个瓶子,3个盖子);

//  上边循环 ,然后又由于 1块钱买不酒 对题目无影响,所以 算法如下
//   4 块钱3瓶 剩1 瓶三盖
        public static int count(int x){
                int sum = 0 ;
                if(x<3){
                        sum = x/ 2;
                }else{
                        //4块 买一 换2 ++++++因为以后都是1瓶3盖,所以买1换2+换1====每两块买1换3;
                        sum = 3 +(x-4)/2*4;
                }
               
                return sum;
               
        }
}
回复 使用道具 举报
樱释空 发表于 2016-8-5 23:19
public class demo10 {
        public static void main(String[] args) {
                int n=7;//人数

你这个没看懂啊,能把思路说下么,结果好像有点不对。
回复 使用道具 举报
敲敲敲dm 发表于 2016-8-5 23:28
递归 思路 辛辛苦苦半天敲的都是 新手,求给我 ,多思考,多敲,多学习我们一起进步:
import java.util.Sc ...

大神,感觉你数学很好,思路很棒,问一下能不能用代码模拟这个过程?
回复 使用道具 举报
大神,能加个好友吗,很厉害,我一直没想到可以同时把剩余瓶盖数和剩余瓶子数当参数传递给下一次递归函数使用,厉害厉害,学习了。
回复 使用道具 举报
阿卜 发表于 2016-8-5 18:48
public class BuyBeers {

        public static void main(String[] args) {


大神,能加个好友吗,我一直在想怎么同时把剩余瓶盖数和剩余瓶子数当参数传递给下一次递归函数使用,没想到,厉害厉害,学习了。
回复 使用道具 举报
jiajun 发表于 2016-8-5 23:57
你这个没看懂啊,能把思路说下么,结果好像有点不对。

这个可以用Scanner输入啊,结果肯定对,不对剁手.
回复 使用道具 举报
阿卜 发表于 2016-8-5 18:48
public class BuyBeers {

        public static void main(String[] args) {

学习学习学习学习学习谢谢
回复 使用道具 举报
好复杂的程序
回复 使用道具 举报
我的笔试题有这个
回复 使用道具 举报
阿卜 发表于 2016-8-5 18:48
public class BuyBeers {

        public static void main(String[] args) {

屌屌屌屌屌屌
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马