ACM算法题

字符串最后一个单词长度

1
2
3
4
5
6
7
8
9
10
11
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner st = new Scanner(System.in);
String cs = st.nextLine();
String[] word = cs.split(" ");
System.out.println(word[word.length - 1].length());
}
}

计算某字符出现次数

1
2
3
4
5
6
7
8
9
10
11
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine().toLowerCase();
String word = in.nextLine().toLowerCase();
System.out.println(str.length() - str.replaceAll(word, "").length());
}
}

*明明的随机数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
TreeSet<Integer> set = new TreeSet<>();
for(int i = 0; i < num; i++){
set.add(scanner.nextInt());
}
Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}

字符串分隔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String str = in.nextLine();
split(str);
}
}
public static void split(String s){
while(s.length() >= 8){
System.out.println(s.substring(0,8));
s = s.substring(8);
}
if(s.length() < 8 && s.length() > 0){
s += "00000000";
System.out.println(s.substring(0,8));
}
}
}

*进制转换

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Scanner;
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
String s = scanner.nextLine();
System.out.println(Integer.parseInt(s.substring(2, s.length()), 16));
}
}
}

质数因子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Main{
public static void main(String[] args) {
// 处理输入
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
// 获取需要求解的值
int target = sc.nextInt();
int y = 2;// 因子从2开始算
while(target != 1){ // 短除法,除到目标值为1为止
if(target % y == 0) // 能能够整除2
{
System.out.print(y+" ");
target /= y;
}else{// 更新y的值
if(y > target / y) y = target;//如果剩余值为质数
else y++; //y值增加1
}
}
}
}
}

取近似值

1
2
3
4
5
6
7
8
9
10
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double num = in.nextDouble();
System.out.println((int) (num + 0.5));
}
}

*合并表记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int tableSize = scanner.nextInt();
Map<Integer, Integer> table = new HashMap<>(tableSize);
for (int i = 0; i < tableSize; i++) {
int key = scanner.nextInt();
int value = scanner.nextInt();
if (table.containsKey(key)) {
table.put(key, table.get(key) + value);
} else {
table.put(key, value);
}
}
for (Integer key : table.keySet()) {
System.out.println( key + " " + table.get(key));
}
}
}

提取不重复的整数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
// 使用HashSet来判断是否是不重复的
HashSet<Integer> hs = new HashSet<>();
int target = sc.nextInt();// 获取代求解的值
while(target != 0){ // 求解每位上面的整数
int temp = target % 10;
if(hs.add(temp)) // 如果能加入,就是说明没有重复
System.out.print(temp);
target /= 10;// 除10能去掉最右边的数字
}
System.out.println();
}
}
}

*字符个数统计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
HashSet<Character> set = new HashSet<>();
for(int i = 0; i < str.length(); i++){
set.add(str.charAt(i));
}
System.out.println(set.size());
}
}

购物单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
int money = sc.nextInt();
int m = sc.nextInt();
sc.nextLine();
money /= 10;
int[][] prices = new int[m+1][3];
int[][] weights = new int[m+1][3];
for (int i = 1; i <= m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
a /= 10;//price
b = b * a;//weight
if (c == 0) {
// 主件
prices[i][0] = a;
weights[i][0] = b;
} else if (prices[c][1] == 0) {
// 附件1
prices[c][1] = a;
weights[c][1] = b;
} else {
// 附件2
prices[c][2] = a;
weights[c][2] = b;
}
sc.nextLine();
}
int[][] dp = new int[m+1][money+1];
for (int i = 1; i <= m; i++) {
for(int j = 1; j <= money; j++) {
int a = prices[i][0];
int b = weights[i][0];
int c = prices[i][1];
int d = weights[i][1];
int e = prices[i][2];
int f = weights[i][2];

dp[i][j] = j - a >= 0 ? Math.max(dp[i-1][j], dp[i-1][j-a] + b) : dp[i-1][j];
dp[i][j] = j-a-c >= 0 ? Math.max(dp[i][j], dp[i-1][j-a-c] + b + d):dp[i][j];
dp[i][j] = j-a-e >= 0 ? Math.max(dp[i][j], dp[i-1][j-a-e] + b + f):dp[i][j];
dp[i][j] = j-a-c-e >= 0 ? Math.max(dp[i][j], dp[i-1][j-a-c-e] + b +d + f):dp[i][j];
}
}

System.out.println(dp[m][money] * 10);
}
}
}

*坐标移动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;
import java.io.*;

public class Main{
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] in = bf.readLine().split(";");
int x = 0;
int y = 0;
for(String s : in){
// 不满足题目给定坐标规则
if(!s.matches("[WASD][0-9]{1,2}")){
continue;
}
int val = Integer.valueOf(s.substring(1));
switch(s.charAt(0)){
case 'W':
y += val;
break;
case 'S':
y -= val;
break;
case 'A':
x -= val;
break;
case 'D':
x += val;
break;
}
}
System.out.println(x+","+y);
}
}

*密码验证合格程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
import java.util.regex.*;
public class Main{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.next();
if(str.length() <= 8){
System.out.println("NG");
continue;
}
if(getMatch(str)){
System.out.println("NG");
continue;
}
if(getString(str, 0, 3)){
System.out.println("NG");
continue;
}
System.out.println("OK");
}
}
// 校验是否有重复子串
private static boolean getString(String str, int l, int r) {
if (r >= str.length()) {
return false;
}
if (str.substring(r).contains(str.substring(l, r))) {
return true;
} else {
return getString(str,l+1,r+1);
}
}
// 检查是否满足正则
private static boolean getMatch(String str){
int count = 0;
Pattern p1 = Pattern.compile("[A-Z]");
if(p1.matcher(str).find()){
count++;
}
Pattern p2 = Pattern.compile("[a-z]");
if(p2.matcher(str).find()){
count++;
}
Pattern p3 = Pattern.compile("[0-9]");
if(p3.matcher(str).find()){
count++;
}
Pattern p4 = Pattern.compile("[^a-zA-Z0-9]");
if(p4.matcher(str).find()){
count++;
}
if(count >= 3){
return false;
}else{
return true;
}
}
}

合唱队

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

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[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

int[] left = new int[n]; //存储每个数左边小于其的数的个数
int[] right = new int[n];//存储每个数右边小于其的数的个数
left[0] = arr[0];
right[n - 1] = arr[n-1];
int num[] =new int [n];//记录以i为终点的从左向右和从右向走的子序列元素个数
int index = 1;//记录当前子序列的长度
for(int i=1;i<n;i++){
if(arr[i]>left[index-1]){
//直接放在尾部
num[i] = index;//i左侧元素个数
left[index++] = arr[i];//更新递增序列
}else {
//找到当前元素应该放在的位置
int low = 0,high = index-1;
while(low < high){
int mid = (low+high)/2;
if(left[mid] <arr[i])
low = mid + 1;
else
high = mid;
}
//将所属位置替换为当前元素
left[low] = arr[i];
num[i] = low;//当前位置i的左侧元素个数
}
}
index = 1;
for(int i=n-2;i>=0;i--){
if(arr[i]>right[index-1]){
num[i] += index;
right[index++] = arr[i];
}else {
int low = 0,high = index-1;
while(low < high){
int mid = (high+low)/2;
if(right[mid]<arr[i])
low = mid+1;
else
high = mid;
}
right[low] = arr[i];
num[i]+=low;
}
}
int max = 1;
for (int number: num )
max = Math.max(max,number);
// max+1为最大的k
System.out.println(n - max);
}
}
}

*字符串排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.io.*;
import java.util.*;

public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String original = "";
while ((original = br.readLine()) != null) {

// 遍历字符串并存入TreeMap中
TreeMap<Character, StringBuilder> map = new TreeMap<>();
for (char c: original.toCharArray()) {
if ('a' <= c && c <= 'z') {
if (!map.containsKey(c)) {
map.put(c, new StringBuilder());
}
map.get(c).append(c);
} else if ('A' <= c && c <= 'Z') {
char k = (char)(c - 'A' + 'a');
if (!map.containsKey(k)) {
map.put(k, new StringBuilder());
}
map.get(k).append(c);
}
}

// 将已排序好的字母部分合并
StringBuilder sb = new StringBuilder();
for (Map.Entry<Character, StringBuilder> entry: map.entrySet()) {
sb.append(entry.getValue());
}

// 重新遍历字符串以插入非字母的字符
for (int i = 0; i < original.length(); i++) {
char other = original.charAt(i);
if ('a' <= other && other <= 'z') {
continue;
} else if ('A' <= other && other <= 'Z') {
continue;
} else {
sb.insert(i, other);
}
}
System.out.println(sb.toString());
}
br.close();
}
}

*删除字符串中出现次数最少的字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Map<Character,Integer> map = new HashMap<>();
while(in.hasNextLine()){
int min = Integer.MAX_VALUE;
String str = in.nextLine();
int len = str.length();
for(int i = 0; i < len; i++){
char ch = str.charAt(i);
if(!map.containsKey(ch)){
map.put(ch,1);
min = Math.min(min,1);
}else{
int num = map.get(ch) + 1;
map.put(ch,num);
if(!map.containsValue(min)){//最小值增大的情况
min = num;
}
}
}
for(int i = 0; i < len; i++){
if(min != map.get(str.charAt(i))){
System.out.print(str.charAt(i));
}
}
System.out.println();
map.clear();
}
}
}

*整数和IP地址间的转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;

public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String s = sc.next();
if(s.contains(".")){
System.out.println(ip2num(s));
}else{
System.out.println(num2ip(Long.parseLong(s)));
}
}
}

public static long ip2num(String ip){
String[] iip = ip.split("\\.");
StringBuilder sb = new StringBuilder();
for(int i=0; i<4; i++){
int num = Integer.parseInt(iip[i]); // 拆分
String num2 = Integer.toBinaryString(num); //转换为二进制
while(num2.length()<8){
num2 = "0" + num2; // 拼接
}
sb.append(num2);
}
return Long.parseLong(sb.toString(), 2); // 转化为10进制
}

public static String num2ip(long num){
String num2 = Long.toBinaryString(num); //转换为2进制
while(num2.length()<32){
num2 = "0" + num2;
}
String[] ans = new String[4];
for(int i=0; i<4; i++){
String s = num2.substring(8*i, 8*i+8); //拆分
s = Integer.toString(Integer.parseInt(s, 2)); //转化为10进制
ans[i] = s;
}
return String.join(".", ans); //拼接
}
}

*输入整型数组和排序标识,对其元素按照升序或降序进行排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int num = in.nextInt();
Integer[] arr = new Integer[num];
for(int i = 0; i < num; i++){
arr[i] = in.nextInt();
}
int flag = in.nextInt();
if(flag == 0){
Arrays.sort(arr, new Comparator<Integer>(){
public int compare(Integer o1, Integer o2){
return o1 - o2;
}
});
}else if(flag == 1){
Arrays.sort(arr,new Comparator<Integer>(){
public int compare(Integer o1,Integer o2){
return o2-o1;
}
});
}

for(Integer n : arr){
System.out.print(n + " ");
}
System.out.println();
}
}
}

*字符逆序

1
2
3
4
5
6
7
8
9
10
11
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
StringBuilder sb = new StringBuilder(str);
System.out.println(sb.reverse());
}
}

*查找兄弟单词

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String[] str = in.nextLine().split(" ");
Integer num = Integer.parseInt(str[0]);
String x = str[str.length - 2];
Integer k = Integer.parseInt(str[str.length - 1]);
List<String> list = new ArrayList<>();

for(int i = 1; i <= num; i++){
if(isBro(x, str[i])){
list.add(str[i]);
}
}
int size = list.size();
System.out.println(size);

if(size >= k){
Collections.sort(list);
System.out.println(list.get(k - 1));
}
}
}
public static boolean isBro(String x, String n){
if(x.length() != n.length() || n.equals(x)){
return false;
}
char[] cs = x.toCharArray();
char[] key = n.toCharArray();
Arrays.sort(cs);
Arrays.sort(key);
return new String(cs).equals(new String(key));
}
}

*成绩排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
HashMap<Integer, String> map = new HashMap<>();
while(in.hasNext()){
int num = Integer.parseInt(in.nextLine());
int flag = Integer.parseInt(in.nextLine());
int[][] score = new int[num][2];
for(int i = 0; i < num; i++){
String[] nameAndScore = in.nextLine().split(" ");
score[i][0] = i;
score[i][1] = Integer.parseInt(nameAndScore[1]);
map.put(i, nameAndScore[0]);
}

Arrays.sort(score, (o1, o2) -> {
if(flag == 0){
return o2[1] - o1[1];
}else{
return o1[1] - o2[1];
}
});

for(int i = 0; i < num; i++){
System.out.println(map.get(score[i][0]) + " " + score[i][1]);
}
}

}
}

*称砝码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
HashSet<Integer> set = new HashSet<>();
while (in.hasNext()) { // 注意 while 处理多个 case
set.add(0);
int n = in.nextInt();
int[] w = new int[n];
int[] nums = new int[n];
for(int i = 0; i < n; i++){
w[i] = in.nextInt();
}
for(int i = 0; i < n; i++){
nums[i] = in.nextInt();
}

for(int i = 0; i < n; i++){
List<Integer> list = new ArrayList<>(set);
for(int j = 1; j <= nums[i]; j++){
for(int k = 0; k < list.size(); k++){
set.add(list.get(k) + w[i] * j);
}
}
}
System.out.println(set.size());
}
}
}

*求最小公倍数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = a;
System.out.println(gcd(a, b, c));
}
public static int gcd(int a, int b, int c){
if(a % b == 0){
return a;
}
return gcd(a + c, b, c);
}
}

*素数伴侣

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();

List<Integer> evenList = new ArrayList<>();
List<Integer> oddList = new ArrayList<>();
for(int i = 0; i < num; i++){
int j = in.nextInt();
if(isEven(j)){
evenList.add(j);
}else{
oddList.add(j);
}
}

int size = evenList.size();
int count = 0;

int[] evensMatch = new int[size];

for(Integer odd : oddList){
int[] used = new int[size];
if(find(odd, evenList, used, evensMatch)){
count++;
}
}
System.out.println(count);
in.close();
}

public static boolean isEven(int n){
if(n % 2 == 0){
return true;
}
return false;
}

public static boolean isPrime(int n){
if(n <= 1){
return false;
}else{
for(int i = 2; i < n; i++){
if(n % i == 0){
return false;
}
}
}
return true;
}

public static boolean find(int odd, List<Integer> evens, int[] used, int[] evenMatch){
for(int i = 0; i < evens.size(); i++){
if(isPrime(odd + evens.get(i)) && used[i] == 0){
used[i] = 1;
if(evenMatch[i] == 0 || find(evenMatch[i], evens, used,evenMatch)){
evenMatch[i] = odd;
return true;
}
}
}
return false;
}
}

*查找组成一个偶数最接近的两个素数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int left = n / 2;
int right = n / 2;
while(left > 2 && right < n - 2){
if(isPrime(left) && isPrime(right) && n == left + right){
break;
}else{
left--;
right++;
}
}
System.out.println(left);
System.out.println(right);
}

public static boolean isPrime(int n){
if(n <= 2){
return false;
}
for(int i = 2; i < n; i++){
if(n % i == 0){
return false;
}
}
return true;
}
}

*数据分类处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
int num = in.nextInt();
String[] arr = new String[num];
for(int i = 0; i < num; i++){
arr[i] = String.valueOf(in.nextInt());
}

int Rnum = in.nextInt();
Set<Integer> set = new TreeSet<>();
for(int i = 0; i < Rnum; i++){
set.add(in.nextInt());
}

StringBuilder res = new StringBuilder();
int count = 0;
for(int i : set){
int j = 0;
Map<Integer, String> map = new TreeMap<>();

for(String str : arr){
if(str.contains(String.valueOf(i))){
map.put(j, str);
}
j++;
}
if(!map.isEmpty()){
if(count > 0){
res.append(" ");
}
res.append(i).append(" ").append(map.size());
count += 2;
for(Map.Entry<Integer, String> entry : map.entrySet()){
count += 2;
res.append(" ").append(entry.getKey()).append(" ").append(entry.getValue());
}
}
}
if(count > 0){
StringBuilder result = new StringBuilder();
result.append(count).append(" ").append(res.toString());
System.out.println(result.toString());
}
}
}
}

汽水瓶

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()){
int bottle = scanner.nextInt();
if(bottle == 0){
break;
}
System.out.println(bottle / 2);
}


}
}