”小明喝汽水“问题的解决方案

今晚无聊之中看到了群里有人讨论了一个类似算法的题目...闲来无事就看了下。。

题干:

1、小明的钱可以购买99瓶汽水。
2、老板搞促销,5个空汽水瓶子可以换2瓶汽水。
问:小明最多能喝多少瓶汽水?

解决方案:

public class figureBottle {
public static void main(String[] args) {

int x = count(99, 5, 2);
int y = count1(99, 99);
System.out.println(x+" :one method");
System.out.println(y+" :anther method");
}

/**
* 功能描述:我的方法
* <br>输入:
* <br>输出:
* <br>创建日期:Nov 4, 2008
*/
public static int count(int num, int five, int two) {
int res = num;
while (num >= five) {
System.out.println("a new turn");
int a = num / five;
System.out.println(a + " :a");
int b = num % five;
System.out.println(b + " :b");
int newNum = (a * two) + b;
System.out.println(newNum + " :newNum");
int newGet = a * 2;
res += newGet;
System.out.println(res + " :res");
num = newNum;
}
return res;
}

/**
* 功能描述:网友的方案
* <br>输入:
* <br>输出:
* <br>创建日期:Nov 4, 2008
*/
public static int count1(int total, int empty) {

int exchange;
while (empty >= 4) {
exchange = empty / 5 * 2;
empty = empty % 5;
empty += exchange;
total += exchange;

}
System.out.println(total);
return total;
}

}



ps:另一个网友的心算如下


99/5=19剩下4
19*2=38
38/5=7剩下3
7*2=14
14/5=2剩下4
2*2=4
4+4+3+4=15
15/5=3
3*2=6
6/5=1
1*2=2
完毕
合计:
99+38+14+4+6+2=163


o(∩_∩)o...算法还是很有趣的....每天一题就更好了...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值