题解 | #汽水瓶#
汽水瓶
http://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
1.分析喝到汽水的瓶数count和空瓶数n的关系
while(n/3>0){
count += n/3;
n=n/3+n%3;
}
count += n/3;
n=n/3+n%3;
}
2.如果空瓶数n=2,还可以再喝一瓶
if(n==2){
count += 1;
}
count += 1;
}
3.如果空瓶数n=1,则count = 0;
4.如果空瓶数n=0,则代表结束。
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int count = 0; if(n == 0){ break; } while(n/3>0){ count += n/3; n=n/3+n%3; } if(n==2){ count += 1; } System.out.println(count); } } }