Huawei's R & D engineers programming that 1

There is such a puzzle: "a store states: three empty soda bottles for a bottle of soda Zhang can have ten empty soda bottles on hand, she can change how many bottles of soda drink the most.?

"The answer is five bottles, as follows:

First with nine empty bottles 3 bottles of soda change, drank three bottles full of empty bottles after drinking four, with three pulls a bottle, drink this bottle full, this time left two empty bottles.

Then you let the boss to lend you a bottle of soda, drink this bottle full, after drinking for a bottle filled with three empty bottles back to the boss.

If there are n the hands of Zhang empty soda bottles, soda bottles can change the maximum number of drink?

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner scaner = new Scanner(System.in);
        while(scaner.hasNext()){
            int num = scaner.nextInt();
            if(num == 0){
                break;
            }
            System.out.println(calc(num));
        }
    }
     
    private static int calc(int num){
        int result = 0;
        while(num > 2){
         int i = num /3;
         int j = num % 3;
         result += i;
         num = i + j;
        }
        if(num == 2){
            result +=1;
        }
        return result;
    }
}

 

Guess you like

Origin www.cnblogs.com/fly1024/p/12577755.html