参考代码:
package com.cfl;
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
int n = -1;
while (n != 0) {
n = input.nextInt();
list.add(n);
}
list.remove(list.size() - 1);
List<Integer> ansList = new ArrayList<Integer>();
for (Integer count : list) {
int tempCount = 0;
int ans = 0;
int sum = 0;
while (tempCount + count != 0 && tempCount + count != 1) {
sum = tempCount + count;
ans += (tempCount + count) / 3;
tempCount = (tempCount + count) / 3;
count = sum % 3;
if (tempCount + count == 2) {
ans++;
count = 0;
tempCount = 0;
}
}
ansList.add(ans);
}
for (Integer count : ansList) {
System.out.println(count);
}
}
}