有这样一道智力题:“某商店规定:三个空汽水瓶
作者:互联网
package com.test; import java.util.Scanner; public class Test105 { public static void main(String[] args) { /* * 有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?” * 答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子, * 用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的, * 喝完以后用3个空瓶子换一瓶满的还给老板。 * 如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝? * **/ Scanner scanner = new Scanner(System.in); StringBuffer sb = new StringBuffer(); //喝的水瓶数 int count=0; for(int i=0;i<10;i++) { int num = scanner.nextInt(); if(num==0) { break; }else { calculator(num); } } System.out.println(sb.toString()); } public static void calculator(int num) { int count=0; System.out.println("空瓶子数量:"+num); int shang; //喝到汽水数量 int yu; // boolean target = true; while(target) { if(num<2) { count+=0; target = false; }else if(num==2) { count+=1; target = false; }else { shang = num/3; //喝到 count+=shang; yu = num%3; num = shang+yu; } } System.out.println(count); } }
标签:汽水,智力题,Scanner,个空,商店,喝掉,瓶子,汽水瓶 来源: https://www.cnblogs.com/ychun/p/14456858.html