ACM下的输入输出 Java版

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] + " ");
        }
    }
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值