本题将会给出
组测试数据,确切数字未知,您需要一直读入直到特定的结尾;您也可以参考 牛客网在线判题系统使用帮助 获得更多的使用帮助。每组测试数据描述如下:
在一行上输入一个整数
,代表小张手上的空汽水瓶数量。特别地,
代表输入结束,您只需要立即退出,不需要针对这种情况进行处理。
对于每一组测试数据,新起一行。输出一个整数,代表小张最多可以喝到的汽水数量。
3 10 81 0
1 5 40
对于第一组测试数据,共有
个空瓶,可以换
瓶汽水。可以证明无法再做任何兑换,因此最多可以喝到
瓶汽水。
对于第二组测试数据:
第一轮兑换,共有
个空瓶。可以换
瓶汽水,余下
个空瓶;
第二轮兑换,刚刚余下
个空瓶、加上刚刚兑换的
瓶汽水喝完,共有
个空瓶。可以换
瓶汽水,余下
个空瓶;
第三轮兑换,刚刚余下
个空瓶、加上刚刚兑换的
瓶汽水喝完、再找老板借
个空瓶,共有
个空瓶。可以换
瓶汽水,余下
个空瓶。喝完之后不要忘记归还借的空瓶。
综上,一共可以喝到
瓶汽水。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (true) { int n = in.nextInt(); if(n == 0) break; System.out.println(n / 2); } in.close(); } }
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { //方法一 public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 List<Integer> list = new ArrayList<>(); while (in.hasNextInt()) { // 注意 while 处理多个 case int val = in.nextInt(); int x = val; int num = 0; while (x >= 3) { int y = x % 3; int z = (x - y) / 3; num += z; x = z + y; } if (x == 2) { num += 1; } if (num > 0) System.out.println(num); } } }
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) break; int cnt = 0; while (n >= 3) { cnt += n / 3; n = n - n / 3 * 3 + n / 3; } if (n == 2) cnt++; System.out.println(cnt); } } }
private static int canDrinkCount(int bottle) { if (bottle <= 1) { return 0; } else { return 1 + canDrinkCount(bottle - 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.hasNextInt()) { int bottle = in.nextInt(); if(bottle==0){ return; } System.out.println(canDrinkCount(bottle)); } } private static int canDrinkCount(int bottle) { if (bottle <= 1) { return 0; } else { return 1 + canDrinkCount(bottle - 2); } } }
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) return; int count = 0; while(a > 2){ count += a/3; a = a/3 + a%3; } if(a == 2) count++; System.out.println(count); } } }
import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { int n = scanner.nextInt(); if (n == 0) { break; // 输入结束标志 } int result = drinkBottles(n); System.out.println(result); } scanner.close(); } public static int drinkBottles(int bottles) { if(bottles == 2) return 1; if (bottles < 2) { // 如果瓶子数量不足3个,则不能再兑换汽水 return 0; } // 用三个空瓶兑换一瓶汽水 int drank = bottles / 3; // 剩余的瓶子数量 int remaining = bottles % 3; // 如果剩下的瓶子加上兑换汽水后剩下的瓶子不足3个,则不能再兑换 if (remaining + drank < 2) { return drank; } if (remaining + drank == 2) { return drank+1; } // 如果剩下的瓶子加上兑换汽水后剩下的瓶子足够3个,则可以再次兑换 return drank + drinkBottles(remaining + drank); } }递归
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) { break; } int count = num / 2; System.out.println(count); } } }
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) {break;} int count = n / 3; int left = count + (n - 3*count); while (left > 2) { count = count + (left / 3); left = (left / 3) + (left % 3); } if(left == 2) { count = count + 1; left = 0; } System.out.println(count); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int totalUseNum = 0;//总共喝了多少瓶 int totalelseNum = 0;//手头总共剩多少瓶子 // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); if (a == 0) { } else { int IntNum = a / 3;//商 totalUseNum = IntNum; int elseNum = a % 3;//余 totalelseNum = elseNum+totalUseNum; for(int i=0;totalelseNum/3>=1;i++){ elseNum = totalelseNum % 3; IntNum = totalelseNum / 3; totalUseNum = totalUseNum+IntNum;//所有的商加起来 totalelseNum = IntNum+elseNum; //System.out.println(totalUseNum); //System.out.println(totalelseNum); }//59 --29 94--47 if(totalelseNum==2){//借1 totalUseNum = totalUseNum+1; } System.out.println(totalUseNum); } } } }
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); } } }