前些天做了阿里巴巴2018年的客户端开发工程师的笔试题, 现在想起来做下笔记。和各位小伙伴分享分享。
-
其中有一道题是这样的 :
阿里巴巴的食堂搞活动促销,已知某饮料1瓶3元钱,
4个瓶盖可以换一瓶,2个空瓶可以换一瓶,则30元
最多可以喝几瓶。
输入:
A //A表示饮料单价
B //表示瓶盖换瓶比
C //C表示空瓶换瓶比
D //D表示给 定的钱数
输出:S
例:
输入:
3
2
30
输出:
35
思路很简单,采用递归的办法。因为每次换了饮料之后又要瓶盖、空瓶。
在这里直接上代码了
import java.util.Scanner;
/**
* Created by zf
* Date: 2017/8/25
*/
public class Main {
public static int count = 0;
public static int mcap = 0;
public static int mbottle = 0;
public static void sum(int nowCap, int nowBottle) {
if (nowCap >= mcap) {
int newGet = nowCap / mcap;
count += newGet;
nowCap = newGet + nowCap % mcap;
nowBottle += newGet;
}
if (nowBottle >= mbottle) {
int newGet = nowBottle / mbottle;
count += newGet;
nowBottle = newGet + nowBottle % mbottle;
nowCap += newGet;
}
if (nowCap >= mcap || nowBottle >= mbottle) {
sum(nowCap, nowBottle);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int res;
int _price;
_price = Integer.parseInt(in.nextLine().trim());
int _cap;
_cap = Integer.parseInt(in.nextLine().trim());
mcap = _cap;
int _emptyBottle;
_emptyBottle = Integer.parseInt(in.nextLine().trim());
mbottle = _emptyBottle;
int _money;
_money = Integer.parseInt(in.nextLine().trim());
count = _money / _price;
sum(count, count);
System.out.println(count);
}
}
网友评论