某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。
小张手上有n个空汽水瓶,她想知道自己最多可以喝到多少瓶汽水。
数据范围:输入的正整数满足
注意:本题存在多组输入。输入的 0 表示输入结束,并不用输出结果。
输入文件最多包含 10 组测试数据,每个数据占一行,仅包含一个正整数 n( 1<=n<=100 ),表示小张手上的空汽水瓶数。n=0 表示输入结束,你的程序不应当处理这一行。
对于每组测试数据,输出一行,表示最多可以喝的汽水瓶数。如果一瓶也喝不到,输出0。
3 10 81 0
1 5 40
样例 1 解释:用三个空瓶换一瓶汽水,剩一个空瓶无法继续交换 样例 2 解释:用九个空瓶换三瓶汽水,剩四个空瓶再用三个空瓶换一瓶汽水,剩两个空瓶,向老板借一个空瓶再用三个空瓶换一瓶汽水喝完得一个空瓶还给老板
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNextLine()) { int input = Integer.valueOf(in.nextLine()); if(input == 0) { break; } int result = 0; while(input>=3) { int num = input/3; int mod = input%3; result += num; input = num+mod; } if(input==2) { result++; } System.out.println(result); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int re = mat(in.nextInt()); if (re > 0) { System.out.println(re); } } } private static int mat(int v) { if (v < 2) { return 0; } if (v == 2) { return 1; } int sum = v / 3; sum += mat(v / 3 + v % 3); return sum; } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); if (a == 0) return; int r = 0; while (a >= 3) { a -= 3; ++ a; ++ r; } if (a == 2) ++ r; System.out.println(r); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); if(a==0){ break; } int b = getResult(a); System.out.println(b); } } public static int getResult(int a){ int result = 0; int last = a; while(last >3){ int temp = last/3; int s = last % 3; last = temp + s ; result += temp; } if(last == 2 || last ==3){ result++; } return result; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); if (n == 0) { continue; } int empty = n; int drink = 0; while (empty >= 2) { if (empty == 2) { drink+=1; empty = 1; continue; } int mod = empty % 3; if (mod != 0) { drink += (empty - mod) / 3; empty = (empty - mod) / 3 + mod; } else { drink += empty / 3; empty = empty/3; } if(empty == 2) empty++; } System.out.println(drink); } } }
public class Demo001 { public static void main(String[] args){ Scanner n = new Scanner(System.in); while(n.hasNextInt()){ int a = n.nextInt();//输入的空汽水瓶数 if(a%3+a/3>=2){ int b = a/3+(a%3+a/3)/2; System.out.println(b); }else { int b = a / 3 + (a % 3 + a / 3) / 3;//计算出来的可以喝到的数量 System.out.println(b); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int num = in.nextInt(); if (num == 0) { return; } int res = 0; int mark = 0; while (num > 1) { if(num ==2){ res ++; num -= 2; }else{ res += num / 3; } mark = num % 3; num = mark + num / 3; } System.out.println(res); } } }
递归解决
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int a = in.nextInt(); if (a <= 0) { continue; } System.out.println(getBottle(a, 3)); } } private static int getBottle(int total, int divisor) { // 差1的时候借1 if (total == divisor - 1) { return 1; } if (total < divisor) { return 0; } int count = total / divisor; return count + getBottle(count + total % divisor, divisor); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case int i=Integer.valueOf(in.nextLine()); if(i!=0){ System.out.println(i/2); } } } }
import java.util.ArrayList; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Integer> plans = new ArrayList<Integer>(); while (true) { int bottles = Integer.parseInt(in.nextLine()); if (bottles == 0)break; plans.add(bottles); } ArrayList<Integer> plansResult = new ArrayList<Integer>(); for (int plane : plans) { int total = 0; while (plane > 2) { int remainde = plane % 3; int currentBottle = plane / 3; total += currentBottle; plane = currentBottle + remainde; } if (plane == 2) { total++; } plansResult.add(total); } plansResult.forEach(System.out::println); } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int num = in.nextInt(); if(num==0){ break; } int count=0; int shang=0; int yushuo=0; while(num>2){ shang =num/3; yushuo=num%3; num=shang+yushuo; count+=shang; } if(num==2) count++; System.out.println(count); } }