OJ online programming common input and output exercises

**Topic 1: * * calculate a+b. The input includes two positive integers a, B (1 < = a, B < = 10 ^ 9), and the input data includes multiple groups.

import java.util.*;

// Class name must be Main
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(sum); // Results must be printed
}
}

public static int add(int a, int b) {
return a + b;
}
}

**Topic 2: * * calculate a+b. Enter that the first row includes a number of data groups t (1 < = T < = 100), and then each row includes two positive integers a and B (1 < = a, B < = 10 ^ 9), for example:

Input:
2
1 5
10 20
Output:
6
30
import java.util.*;

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

public static int add(int a, int b) {
return a + b;
}
}

**Topic 3: * * calculate a+b. 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, 0 will end the input. For example:

Input:
1 5
10 20
0 0 // End input
Output:
6
30
import java.util.*;

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

public static int add(int a, int b) {
return a + b;
}
}

**Topic 4: * * calculate the sum of a series of numbers. The input data includes multiple groups, with one row for each group of data. The first integer of each row is the number of integers in the group n (1 < = n < = 100), followed by n positive integers, that is, n positive integers required to sum. End the input when n is 0. For example:

Input:
4 1 2 3 4
5 1 2 3 4 5
0 // End input
Output:
10
15
import java.util.*;

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

**Topic 5: * * calculate the sum of a series of numbers. The first line entered is a positive integer t (1 < = T < = 100), representing the number of data groups. Next t rows, each representing a set of data. The first integer of each row is the number n of the group of data (1 < = n < = 100), and the next n positive integers are each positive integer that needs to be summed in the group. For example:

Input:
2
4 1 2 3 4
5 1 2 3 4 5
Output:
10
15
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int sum = 0;
for (int j = 0; j < n; j++) {
sum += sc.nextInt();
}
System.out.println(sum); // Be sure to print the results
}
}
}

**Topic 6: * * calculate the sum of a series of numbers. There are multiple groups of input data, and each row represents a group of input data. The first integer in each row is the number n of the group of data (1 < = n < = 100), followed by n positive integers, that is, each positive integer that needs to be summed in the group. For example:

Input:
4 1 2 3 4
5 1 2 3 4 5
Output:
10
15
import java.util.*;

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

**Title 7: * * calculate the sum of a series of numbers. 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).

Input:
1 2 3
4 5
0 0 0 0 0
Output:
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 str = sc.nextLine();
String[] strs = str.split(" ");
int sum = 0;
for (int i = 0; i < strs.length; i++) {
sum += Integer.parseInt(strs[i]);
}
System.out.println(sum);
}
}
}

**Summary: * * basic data type - > string. Use the valueOf() static method in the string class. The parameters of this method are basic data types, such as string valueOf(int i)； String - > basic data type. The basic data type is used to wrap the static methods in the class, such as integer parseInt(String s).

**Title 8: * * output after sorting the input string. The input has two lines. The first line, n, represents the number of strings. The second line is a string separated by n spaces. Output a row of sorted string, separated by spaces, with no spaces at the end. For example:

Input:
5
c d a bb e
Output:
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 = sc.nextInt();
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.next(); // The next() method treats the space, tab, and enter keys as terminators
}
sort(arr); // sort
// output
for (int i = 0; i < n; i++) {
if (i == n - 1) {
System.out.print(arr[i]);
} else {
System.out.print(arr[i] + " ");
}
}
}

// Selective sorting
public static void sort(String[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[i].compareTo(arr[j]) > 0) {
index = j;
}
}
if (index != i) {
String temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}
}

**Title 9: * * output after sorting the input string. Multiple test cases, one line for each test case, separated by spaces, with n strings, n < 100. For each set of test cases, output a row of sorted strings, each separated by a space. For example:

Input:
a c bb
f dddd
nowcoder
Output:
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()) {
String str = sc.nextLine();
String[] arr = str.split(" ");
sort(arr); // See alternative ranking in topic 8
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr[i]);
} else {
System.out.print(arr[i] + " ");
}
}
if (sc.hasNextLine()) {
System.out.println();
}
}
}
}

**Title 10: * * output after sorting the input string. Multiple test cases, one line for each test case, each line passing, separated, with n strings, n < 100. For each group of use cases, output a row of sorted string separated by ',' without ending space. For example:

Input:
a,c,bb
f,dddd
nowcoder
Output:
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()) {
String str = sc.nextLine();
String[] arr = str.split(",");
sort(arr); // See alternative ranking in topic 8
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr[i]);
} else {
System.out.print(arr[i] + ",");
}
}
if (sc.hasNextLine()) {
System.out.println();
}
}
}
}

**Title 11: * * the local self-test passed, but the submission pass rate was 0. 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** Please look at the following question: * * there are multiple groups of test cases in the input, each group is separated by two integers, and the sum of two integers in each group is output.

import java.util.*;

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

**Note: * * the error prone part of this question is to find the sum of two integers. Most people will use hasNextInt() and nextInt(), but if it is a large number, overflow will occur. Therefore, you need to use long type, that is, replace all ints in the code with long.

Conclusion: if this blog is helpful to you, please like, collect or pay attention. Your encouragement is the driving force for bloggers' progress. Thank you for your support and common progress.

Added by bav on Mon, 27 Dec 2021 07:21:49 +0200