OJ online programming common input and output

Posted by shdt on Sat, 04 Sep 2021 20:54:48 +0200

A
The input includes two positive integers a, B (1 < = a, B < = 10 ^ 9), and the input data includes multiple groups.
Output Description:
Output the result of a+b
Example 1
input
1 5
10 20
output
6
30

import java.util.Scanner;
public class Main1 {
    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);
        }
    }
}

B
Calculate a+b
Open the following link to view the correct code
https://ac.nowcoder.com/acm/contest/5657#question
Enter Description:
Enter the number of data groups t included in the first row (1 < = T < = 100)
Next, each line includes two positive integers a, B (1 < = a, B < = 10 ^ 9)
Output Description:
Output the result of a+b
Example 1
input
2
1 5
10 20
output
6
30

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

C
Calculate a+b
Open the following link to view the correct code
https://ac.nowcoder.com/acm/contest/5657#question
Enter Description:
The input includes two positive integers a and B (1 < = a, B < = 10 ^ 9). There are multiple groups of input data. If the input is 0, the input will be ended
Output Description:
Output the result of a+b
Example 1
input
1 5
10 20
0 0
output
6
30

import java.util.Scanner;

public class Main3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String str = sc.nextLine();
            String[] inputs = str.split(" ");
            int a = Integer.parseInt(inputs[0]);
            int b = Integer.parseInt(inputs[1]);
            if (a == 0 && b == 0)
                break;
            System.out.println(a+b);
        }
    }
}

D
Calculate the sum of a series of numbers
Open the following link to view the correct code
https://ac.nowcoder.com/acm/contest/5657#question
Enter Description:
The input data includes multiple groups.
Each group of data has one row. The first integer of each row is the number of integers n (1 < = n < = 100). When n is 0, input ends.
Next, there are n positive integers, that is, each positive integer that needs to be summed.
Output Description:
Each group of data outputs the result of summation
Example 1
input
4 1 2 3 4
5 1 2 3 4 5
0
output
10
15

import java.util.Scanner;

public class Main4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String s = sc.nextLine();
            String[] str = s.split(" ");
            int  n = Integer.parseInt(str[0]);
            if (n == 0)
                break;
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += Integer.parseInt(str[i]);
            }
            System.out.println(sum);
        }

    }

}

E
Calculate the sum of a series of numbers
Open the following link to view the correct code
https://ac.nowcoder.com/acm/contest/5657#question
Enter Description:
The first line of input includes a positive integer t (1 < = T < = 100), indicating the number of data groups.
Next t rows, one set of data per row.
The first integer in each line is the number of integers n (1 < = n < = 100).
Next, there are n positive integers, that is, each positive integer that needs to be summed.
Output Description:
Each group of data outputs the result of summation
Example 1
input
2
4 1 2 3 4
5 1 2 3 4 5
output
10
15

import java.util.Scanner;

public class Main5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < t; i++) {
            String s = sc.nextLine();
            String[] str = s.split(" ");
            int n = Integer.parseInt(str[0]);
            int sum = 0;
            for (int j = 1; j <= n; j++) {
                sum += Integer.parseInt(str[j]);
            }
            System.out.println(sum);
        }
    }
}

F
Calculate the sum of a series of numbers
Open the following link to view the correct code
https://ac.nowcoder.com/acm/contest/5657#question
Enter Description:
There are multiple groups of input data, and each row represents a group of input data.
The first integer in each line is the number of integers n (1 < = n < = 100).
Next, there are n positive integers, that is, each positive integer that needs to be summed.
Output Description:
Each group of data outputs the result of summation
Example 1
input
4 1 2 3 4
5 1 2 3 4 5
output
10
15

import java.util.Scanner;

public class Main6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String s = sc.nextLine();
            String[] str = s.split(" ");
            int n = Integer.parseInt(str[0]);
            int sum =0;
            for (int i = 1; i <= n ; i++){
                sum += Integer.parseInt(str[i]);
            }
            System.out.println(sum);
        }
    }
}

G
Calculate the sum of a series of numbers
Open the following link to view the correct code
https://ac.nowcoder.com/acm/contest/5657#question
Enter Description:
There are multiple groups of input data, and each row represents a group of input data.
Each line may have n integers separated by spaces( 1 <= n <= 100).
Output Description:
Each group of data outputs the result of summation
Example 1
input
1 2 3
4 5
0 0 0 0 0
output
6
9
0

import java.util.Scanner;

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

H
Sort the input string and output it
Open the following link to view the correct code
https://ac.nowcoder.com/acm/contest/5657#question
Enter Description:
The input has two lines, the first line n
The second line is a string separated by n spaces
Output Description:
Output a row of sorted string, separated by spaces and without ending spaces
Example 1
input
5
c d a bb e
output
a bb c d e

import java.util.Arrays;
import java.util.Scanner;

public class Main8 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] st = new String[n];
        for(int i = 0; i < n; i++) {
            st[i] = sc.next();
        }
        Arrays.sort(st);
        for(int i = 0; i < n; i++) {
            if(i == 0) System.out.print(st[i]);
            else System.out.print(" "+st[i]);
        }
    }
}

I
Sort the input string and output it
Open the following link to view the correct code
https://ac.nowcoder.com/acm/contest/5657#question
Enter Description:
Multiple test cases, one line for each test case.
Each line is separated by spaces, with n characters, n < 100, output Description:
For each set of test cases, output a row of sorted strings, each separated by a space
Example 1
input
a c bb
f dddd
nowcoder
output
a bb c
dddd f
nowcoder

import java.util.Arrays;
import java.util.Scanner;

public class Main9 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()){
            String[] str = sc.nextLine().split(" ");
            Arrays.sort(str);
            for (int i = 0; i <str.length; i++) {
                if (i == 0){
                    System.out.print(str[i]+" ");
                }
                else{
                    System.out.print(str[i]+" ");
                }
            }
            System.out.println();
        }
    }
}

J
Sort the input string and output it
Open the following link to view the correct code
https://ac.nowcoder.com/acm/contest/5657#question
Enter Description:
Multiple test cases, one line for each test case.
Each line is separated by, with n characters, n < 100
Output Description:
For each group of use cases, output a row of sorted string separated by ',' without ending space
Example 1
input
a,c,bb
f,dddd
nowcoder
output
a,bb,c
dddd,f
nowcoder

import java.util.Arrays;
import java.util.Scanner;

public class Main10 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()){
            String[] str = sc.nextLine().split(",");
            Arrays.sort(str);
            for (int i = 0; i < str.length; i++) {
                if (i < str.length-1)
                    System.out.print(str[i]+",");
                else
                    System.out.println(str[i]);
            }
        }
    }
}

K
Link: https://ac.nowcoder.com/acm/contest/5657/K
Source: niuke.com
Enter Description:
The input has multiple groups of test cases, with each group of spaces separated by two integers
Output Description:
For each group of data, the sum of two integers in a row is output
Example 1
input
1 1
output
2

import java.util.Scanner;

public class Main11 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()){
            String[] str = sc.nextLine().split(" ");
            long sum = 0;
            for (int i = 0; i < str.length; i++) {
                sum += Long.parseLong(str[i]);
            }
            System.out.println(sum);
        }
    }
}

Topics: Java Algorithm data structure