酒瓶和啤酒的问题

xiaoxiao2021-02-28  111

一、题目

每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。10元最多可喝多少瓶啤酒?

二、程序 package test; public class Solution { public static int all = 5; public static int leftBottle; public static int leftTop; public static void main(String[] args) { all = all + drink(5,5); System.out.println("总共能喝"+all); System.out.println("剩下酒瓶个数"+leftBottle); System.out.println("剩下盖子个数"+leftTop); } public static int drink(int bottle, int top) { if(bottle >=2 || top >=4){ leftBottle = (bottle/2)+(bottle%2)+(top/4); leftTop = (top/4) + (top%4) + (bottle/2); return (bottle/2) + (top/4) + drink(leftBottle,leftTop); } return 0; } } 执行结果如下 总共能喝15 剩下酒瓶个数1 剩下盖子个数3

转载请注明原文地址: https://www.6miu.com/read-60036.html

最新回复(0)