ACM下的输入输出 Java版

  • 1. 多组空格分割的两个整数 (无行数,结束字符限制)
  • 2. 第一行组数接空格分割的两个正整数 (行数限制)
  • 3. 空行分割的两个正整数,0 0 结束 (结束符限制)
  • 4. 每行第一个为个数后带空格分割整数,0结束 (结束符限制,每行有个数限制)
  • 5. 第一行组数接第一个个数接空格分开的整数 (行数限制,每行有个数限制)
  • 6. 每行第一个为个数后带空格分割整数 (无结束限制,每行有个数限制)
  • 7. 多组空格分隔的正整数 (无结束限制,每行无个数限制,需要当作字符串处理)
  • 8. 第一行个数第二行字符串
  • 9. 多行空格分开的字符串
  • 10. 多行逗号分开的字符串 (逗号和空格的区别就是最后一个字符输完的时候",“还是” " )
  • 11. 数组排序


1. 多组空格分割的两个整数 (无行数,结束字符限制)

输入:

1 5
10 20

输出:

6
30

示例

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { 
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
}

2. 第一行组数接空格分割的两个正整数 (行数限制)

输入:

2
1 5
10 20

输出:

6
30

示例

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
}

3. 空行分割的两个正整数,0 0 结束 (结束符限制)

输入:

1 5
10 20
0 0

输出:

6
30

示例

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        while (in.hasNext()) { 
            int a = in.nextInt();
            int b = in.nextInt();
            if(a ==0 && b == 0) break;
            System.out.println(a + b);
        }
    }
}

4. 每行第一个为个数后带空格分割整数,0结束 (结束符限制,每行有个数限制)

输入:

4 1 2 3 4
5 1 2 3 4 5
0

输出:

10
15

示例

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        while (in.hasNext()) { 
            int n = in.nextInt();
            if(n == 0) break;
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += in.nextInt();
            }
            System.out.println(sum);
        }
    }
}

5. 第一行组数接第一个个数接空格分开的整数 (行数限制,每行有个数限制)

输入:

2
4 1 2 3 4
5 1 2 3 4 5

输出:

10
15

示例

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        
        int num = in.nextInt();
        for (int i = 0; i < num; i++){
            int n = in.nextInt();
            int sum = 0;
            for(int j = 0; j < n; j++){
                sum += in.nextInt();
            }
            System.out.println(sum);
        }
    }
}

6. 每行第一个为个数后带空格分割整数 (无结束限制,每行有个数限制)

输入:

4 1 2 3 4
5 1 2 3 4 5

输出:

6
30

示例

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();
            int sum = 0;
            while (n > 0) {
                sum += in.nextInt();
                n--;
            }
            System.out.println(sum);
        }
    }
}

7. 多组空格分隔的正整数 (无结束限制,每行无个数限制,需要当作字符串处理)

输入:

1 2 3
4 5
0 0 0 0 0

输出:

6
9
0

示例

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { 
            String[] s = in.nextLine().split(" ");
            int sum = 0;
            for (int i = 0; i < s.length; i++) {
                sum += Integer.parseInt(s[i]);
            }
            System.out.println(sum);
        }
    }
}

8. 第一行个数第二行字符串

输入:

5
c d a bb e

输出:

a bb c d e

示例

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        while (in.hasNext()) { 
            String[] s = in.nextLine().split(" ");
            Arrays.sort(s);
            for (int i = 0; i < s.length; i++) {
                System.out.print(s[i] + " ");
            }
            
        }
    }
}

9. 多行空格分开的字符串

输入:

a c bb
f dddd
nowcoder

输出:

a bb c
dddd f
nowcoder

示例

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { 
            String[] s = in.nextLine().split(" ");
            Arrays.sort(s);
            for (String c:s) {
                System.out.print(c + " ");
            }
            System.out.println();
        }
    }
}

10. 多行逗号分开的字符串 (逗号和空格的区别就是最后一个字符输完的时候",“还是” " )

输入:

a,c,bb
f,dddd
nowcoder

输出:

a,bb,c
dddd,f
nowcoder

示例

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { 
            String[] s = in.nextLine().split(",");
            Arrays.sort(s);
            int l = s.length;
            for (int i = 0; i < l - 1; i++) {
                System.out.print(s[i] + ",");
            }
            System.out.println(s[l-1]);
        }
    }
}

11. 数组排序

输入:

4 5 1 2 3

输出:

1 2 3 5

示例

public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner (new BufferedInputStream(System.in));
            int n = cin.nextInt();
            int a[] = new int [n];
            for (int i = 0; i < n; i++) a[i] = cin.nextInt();
            Arrays.sort(a);
            for (int i = 0; i < n; i++) System.out.print(a[i] + " ");
        }
    }