技术JavaACM 输入输出
penjc1. 多行输入,每行两个整数
1 2 3 4 5 6 7 8 9 10 11 12
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int a = scanner.nextInt(); int b = scanner.nextInt(); System.out.println(a + b); } } }
|
2. 多组数据,每组第一行为n,之后输入n行两个整数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); while (n-- > 0) { int a = scanner.nextInt(); int b = scanner.nextInt(); System.out.println(a + b); } } } }
|
3. 若干行输入,每行输入两个整数,遇到特定条件终止
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int a = scanner.nextInt(); int b = scanner.nextInt(); if (a == 0 && b == 0) { break; } System.out.println(a + b); } } }
|
4. 若干行输入,遇到0终止,每行第一个数为N,表示本行后面有N个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); if (n == 0) break; int sum = 0; for (int i = 0; i < n; i++) { sum += scanner.nextInt(); } System.out.println(sum); } } }
|
5. 多行输入,每行包含两个整数a和b,每行输出后接一个空行
1 2 3 4 5 6 7 8 9 10 11 12 13
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int a = scanner.nextInt(); int b = scanner.nextInt(); System.out.println(a + b); System.out.println(); } } }
|
6. 多组n行数据,每行先输入一个整数N,接着输入N个整数,每组输出之间输出一个空行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int N = scanner.nextInt(); while (N-- > 0) { int M = scanner.nextInt(); int sum = 0; while (M-- > 0) { sum += scanner.nextInt(); } System.out.println(sum); if (N > 0) System.out.println(); } } } }
|
7. 多组测试样例,每组输⼊数据为字符串,字符⽤空格分隔,输出为⼩数点后两位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import java.util.*;
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String line = in.nextLine(); String[] items = line.split(" "); for (String item : items) { double num = Double.parseDouble(item); System.out.printf("%.2f ", num); } System.out.println(); } } }
|
8. 多组测试⽤例,第⼀⾏为正整数n, 第⼆⾏为n个正整数,n=0时,结束输⼊,每组输出结果的下⾯都输出⼀个空⾏
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import java.util.ArrayList; import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { Integer size = scanner.nextInt(); if (size == 0) { break; } ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < size; i++) { int num = scanner.nextInt(); list.add(num); } for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println(res); System.out.println(); } } }
|
9. 多组测试数据,每组数据只有⼀个整数,对于每组输⼊数据,输出⼀⾏,每组数据下⽅有⼀个空⾏。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.util.*;
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); while (n > 0) { int tmp = n % 10; n /= 10; } System.out.println(res); System.out.println(); } } }
|
10. 多组测试数据,每个测试实例包括2个整数M,K(2<=k<=M<=1000)。M=0,K=0代表输⼊结束。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int m = scanner.nextInt(); int k = scanner.nextInt(); if (m == 0 && k == 0) break; int sum = m + m / k; System.out.println(sum); } } }
|
11. 多组测试数据,⾸先输⼊⼀个整数N,接下来N⾏每⾏输⼊两个整数a和b, 读取输⼊数据到Map
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import java.util.Scanner; import java.util.HashMap; import java.util.Map;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Map<Integer, Integer> map = new HashMap<>(); while (scanner.hasNext()) { int n = scanner.nextInt(); for (int i = 0; i < n; i++) { int a = scanner.nextInt(); int b = scanner.nextInt(); map.put(a, b); } } } }
|
12. 打印数字图形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print(j); } for (int j = i - 1; j >= 1; j--) { System.out.print(j); } System.out.println(); } } }
|
13. 每行输入一个字符和一个整数,遇到特殊字符结束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String line = sc.nextLine(); if (line.equals("@")) break; String[] inputs = line.split(" "); char ch = inputs[0].charAt(0); int n = Integer.parseInt(inputs[1]); } sc.close(); } }
|
14. 第一个输入一个整数n,之后输入n行字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine(); for (int i = 0; i < n; i++) { String str = scanner.nextLine().trim(); StringBuilder sb = new StringBuilder(); System.out.println(sb.toString()); } } }
|
15. 每组测试数据的第一个输入一个字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine(); for (int i = 0; i < n; i++) { String s1 = scanner.nextLine(); String s2 = scanner.nextLine(); System.out.println(s1 + s2); } } }
|
16. 多组测试数据,每组测试数据的第一个输入n,接着输入n组数据,每组输出一个字符串
1 2 3 4 5 6 7 8 9 10 11 12 13
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine(); for (int i = 0; i < n; i++) { String str = scanner.nextLine(); System.out.println(str); } } }
|
17. 多组测试数据,第一个输入n,接下来是n个正整数,判断出栈合法性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import java.util.Scanner; import java.util.Stack;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); if (n == 0) break; int[] sequence = new int[n]; for (int i = 0; i < n; i++) { sequence[i] = scanner.nextInt(); } Stack<Integer> stack = new Stack<>(); boolean valid = true; for (int i = 0; i < n; i++) { stack.push(sequence[i]); if (!stack.isEmpty() && stack.peek() == sequence[i]) { stack.pop(); } else { valid = false; break; } } System.out.println(valid ? "Yes" : "No"); } } }
|
以下是第18到24的标题及其对应的 Java 代码:
18. 输入多组测试数据
1 2 3 4 5 6 7 8 9 10 11 12 13
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); if (n == 0) break; System.out.println("处理逻辑"); } } }
|
19. 字符串处理,输出特定的字符串
1 2 3 4 5 6 7 8 9 10 11 12
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String input = scanner.nextLine(); System.out.println("处理后的字符串:" + input); } } }
|
20. 数值运算问题
1 2 3 4 5 6 7 8 9 10 11 12 13
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int a = scanner.nextInt(); int b = scanner.nextInt(); System.out.println("和:" + (a + b)); } } }
|
21. 对字符串进行拼接
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String str1 = scanner.nextLine(); String str2 = scanner.nextLine(); String result = str1 + str2; System.out.println(result); } } }
|
22. 数学问题(计算和/差/积/商等)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int a = scanner.nextInt(); int b = scanner.nextInt(); System.out.println("和:" + (a + b)); System.out.println("差:" + (a - b)); System.out.println("积:" + (a * b)); if (b != 0) { System.out.println("商:" + (a / b)); } else { System.out.println("除法无法进行"); } } } }
|
23. 处理特定条件下的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); if (n > 0) { System.out.println("输入是正数"); } else if (n < 0) { System.out.println("输入是负数"); } else { System.out.println("输入是零"); } } } }
|
24. 模拟栈、队列操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import java.util.Scanner; import java.util.Stack; import java.util.LinkedList; import java.util.Queue;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Stack<Integer> stack = new Stack<>(); System.out.println("请输入栈操作:"); stack.push(1); stack.push(2); System.out.println("栈顶元素:" + stack.peek()); stack.pop(); System.out.println("栈顶元素:" + stack.peek());
Queue<Integer> queue = new LinkedList<>(); System.out.println("请输入队列操作:"); queue.offer(1); queue.offer(2); System.out.println("队列前端元素:" + queue.peek()); queue.poll(); System.out.println("队列前端元素:" + queue.peek()); } }
|