今晚无聊之中看到了群里有人讨论了一个类似算法的题目...闲来无事就看了下。。
题干:
1、小明的钱可以购买99瓶汽水。
2、老板搞促销,5个空汽水瓶子可以换2瓶汽水。
问:小明最多能喝多少瓶汽水?
解决方案:
ps:另一个网友的心算如下
o(∩_∩)o...算法还是很有趣的....每天一题就更好了...
题干:
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...算法还是很有趣的....每天一题就更好了...