ACM输入输出(Java实现)- 牛客OJ

本文详细介绍了如何在Java中对字符串进行排序,并提供了多个实例,包括字符串数组排序和字符逐行处理。同时涵盖了基础的数字运算,如加法计算和处理多组测试用例。通过实例学习了如何处理不同输入格式和输出要求。
摘要由CSDN通过智能技术生成


牛客: OJ在线编程常见输入输出练习场

一、字符串

1.1 字符串排序

题目描述
对输入的字符串进行排序后输出
输入描述:
输入有两行,第一行n
第二行是n个字符串,字符串之间用空格隔开
输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格
示例:
输入
5
c d a bb e
输出
a bb c d e

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        String[] strs = sc.nextLine().split(" ");
        Arrays.sort(strs);
        System.out.print(String.join(" ", strs));
    }
}

1.2 字符串排序

题目描述
对输入的字符串进行排序后输出
输入描述:
多个测试用例,每个测试用例一行。
每行通过空格隔开,有n个字符,n<100
输出描述:
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开
示例:
输入
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 sc = new Scanner(System.in);
        while(sc.hasNextLine()) { // 也可用 hasNext
            String[] strs = sc.nextLine().split(" ");
            Arrays.sort(strs);
            System.out.println(String.join(" ", strs));
        }
    }
}

二、数字

2.1 a + b

题目描述
计算a+b
输入描述:
输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。
输出描述:
输出a+b的结果
示例:
输入
1 5
10 20
输出
6
30

import java.util.*;

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

2.2 a + b

题目描述
计算a+b
输入描述:
输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 1000)
输出描述:
输出a+b的结果
示例:
输入
2
1 5
10 20
输出
6
30

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        while (sc.hasNext()) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            
            System.out.println(a + b);
        }
    }
}

2.3 a + b

题目描述
计算a+b
输入描述:
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
输出描述:
输出a+b的结果
示例:
输入
1 5
10 20
0 0
输出
6
30

import java.util.*;

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

2.4 a + b

题目描述
计算a+b
输入描述:
输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果
示例:
输入
4 1 2 3 4
5 1 2 3 4 5
0
输出
10
15

import java.util.*;

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

2.5 a + b

题目描述
计算a+b
输入描述:
输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果
示例:
输入
2
4 1 2 3 4
5 1 2 3 4 5
输出
10
15

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        // while (sc.hasNext()) { 
        for (int j = 0; j < t; j++) {
            int n = sc.nextInt();
            if (n == 0) {
                break;
            }
            int res = 0;
            for (int i = 0; i < n; i++) {
                res += sc.nextInt();
            }
            
            System.out.println(res);
        }
    }
}

2.6 a + b

题目描述
计算a+b
输入描述:
输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果
示例:
输入
4 1 2 3 4
5 1 2 3 4 5
输出
10
15

import java.util.*;

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

2.7 a + b

题目描述
计算a+b
输入描述:
输入数据有多组, 每行表示一组输入数据。
每行不定有n个整数,空格隔开。(1 <= n <= 100)。
输出描述:
每组数据输出求和的结果
示例:
输入
1 2 3
4 5
0 0 0 0 0
输出
6
9
0

import java.util.*;

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

三、自测本地通过提交为0

题目描述
计算a+b
输入描述:
输入有多组测试用例,每组空格隔开两个整数
输出描述:
对于每组数据输出一行两个整数的和
示例:
输入
1 1
输出
2

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
//             Long a = sc.nextLong();
//             Long b = sc.nextLong();
            
//             System.out.println(a + b);
            System.out.println(sc.nextLong() + sc.nextLong());
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心海非海_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值