OJ input and output - Analysis and code (Java)

1. [programming question] A+B(1)

(1) Title

Calculate a+b

Enter Description:

  • 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

Input example 1:

1 5
10 20

Output example 1:

6
30

(2) Code

import java.util.*;
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();
            int ans = a + b;
            System.out.println(ans);
        }
        return;
    }
}

2. [programming question] A+B(2)

(1) Title

Calculate a+b

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

Input example 1:

2
1 5
10 20

Output example 1:

6
30

(2) Code

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

3. [programming question] A+B(3)

(1) Title

Calculate a+b

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

Input example 1:

1 5
10 20
0 0

Output example 1:

6
30

(2) Code

import java.util.*;
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)
                return;
            System.out.println(a + b);
        }
        return;
    }
}

4. [programming question] A+B(4)

(1) Title

Calculate the sum of a series of numbers

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

Input example 1:

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

Output example 1:

10
15

(2) Code

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

5. [programming question] A+B(5)

(1) Title

Calculate the sum of a series of numbers

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

Input example 1:

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

Output example 1:

10
15

(2) Code

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

6. [programming question] A+B(6)

(1) Title

Calculate the sum of a series of numbers

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

Input example 1:

4 1 2 3 4
5 1 2 3 4 5

Output example 1:

10
15

(2) Code

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

7. [programming question] A+B(7)

(1) Title

Calculate the sum of a series of numbers

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

Input example 1:

1 2 3
4 5
0 0 0 0 0

Output example 1:

6
9
0

(2) Code

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String line = in.nextLine();
            String[] nums = line.split(" ");
            int ans = 0;
            for (String num : nums) {
                ans += Integer.parseInt(num);
            }
            System.out.println(ans);
        }
        return;
    }
}

8. [programming question] string sorting (1)

(1) Title

Sort the input string and output it

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

Input example 1:

5
c d a bb e

Output example 1:

a bb c d e

(2) Code

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

9. [programming question] string sorting (2)

(1) Title

Sort the input string and output it

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

Input example 1:

a c bb
f dddd
nowcoder

Output example 1:

a bb c
dddd f
nowcoder

(2) Code

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

10. [programming question] string sorting (3)

(1) Title

Sort the input string and output it

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

Input example 1:

a,c,bb
f,dddd
nowcoder

Output example 1:

a,bb,c
dddd,f
nowcoder

(2) Code

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

11. [programming question] self test local pass submission is 0

(1) Title

In the first few online written test programming questions every year, some students always ask why I passed the local test and the self-test, but the code submission system returns a pass rate of 0.

It's not the system's fault, it may be because

  1. You misunderstood the topic. Your code only passed the sample or your own data
  2. There is a problem with your code logic. Your code only passes through the sample or your own data

In short, your code has only passed the sample and self-test data. You can't see the background test data at all. Think more about it yourself.

If the pass rate of this topic is 0 after submission and you feel that your code is correct, you can click to view the passed code

Remember:
When you suspect that there is a problem with the system or subject data during the written test, be sure to doubt your code first!
When you suspect that there is a problem with the system or subject data during the written test, be sure to doubt your code first!

Please send this exercise topic to your friends and classmates. Thank you

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

Input example 1:

1 1

Output example 1:

2

(2) Code

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            long ans = in.nextLong() + in.nextLong();
            System.out.println(ans);
        }
        return;
    }
}

Tags: Java data structure

Posted on Fri, 03 Sep 2021 13:11:36 -0400 by drak