本帖最后由 sisel 于 2015-4-9 20:52 编辑
刚才口算错了。。算法应该是28-9@3=19把问题看复杂了- package com.itheima;
- public class Drink {
- public static void main(String[] args) {
- System.out.println(tryGetDrinks(29));
- }
- private static int tryGetDrinks(int drinks) {
- int tryi=drinks/2;
- for (; tryi<drinks; tryi++) {
- int atAllDrinked = new DrinkParty(tryi).atAllDrinked();
- if(atAllDrinked>=drinks){
- return tryi;
- }
- }
- return -1;
- }
-
-
- }
- class DrinkParty{
- int drinked,buyed,caps;
- public DrinkParty(int buyed) {
- super();
- this.buyed = buyed;
- this.drinked=buyed;
- this.caps=buyed;
- }
-
- private boolean more(){
- return caps>=3;
- }
-
- private void rollAndDrink(){
- System.out.println(this);
- int rolled = caps/3;
- this.drinked+=rolled;
- caps=caps%3+rolled;
- }
-
- @Override
- public String toString() {
- return "[buyed:\t"+buyed+"\tdrinked:\t"+drinked+"\tcap:\t"+caps+"\t]";
- }
- public int atAllDrinked(){
- while(this.more()){
- this.rollAndDrink();
- }
- return drinked;
- }
- }
复制代码
|