有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext())
{
int a=scanner.nextInt();
int count=0;
if(a==0)//边界
break;
while(a!=0&&a!=1){//大于0和1
if(a>2){
count=a/3+count;//统计
a=a/3+a%3;
}
if(a==2){
count=count+1;//统计
a=0;
}
}
System.out.println(count);
}
}
}
OR:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext())
{
int a=scanner.nextInt();
if(a==0)//边界
break;
System.out.println(a/2);
}
}
}