先上链接:https://www.nowcoder.com/test/27976983/summary#question 之前刷题一直在leetcode上面,输入输出都是安排好的,非常省事,但是到了笔试的时候就发现非常的不适应,大部分时间花在了输入输出上。因此特地花时间练了一下输入输出,建议大家也练一下,确实能取到事半功倍的效果! [编程题]A+B(1) 输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a + b);
}
}
}
[编程题]A+B(2) 输入第一行包括一个数据组数t(1 <= t <= 100) 接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a + b);
}
}
}
[编程题]A+B(3) 输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int a = scanner.nextInt();
int b = scanner.nextInt();
if (a == 0 && b == 0) {
return;
}
System.out.println(a + b);
}
}
}
[编程题]A+B(4) 输入数据包括多组。 每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。 接下来n个正整数,即需要求和的每个正整数。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int n = scanner.nextInt();
if (n == 0) {
return;
}
int sum = 0;
for (int i = 0; i < n; i++) {
int anInt = scanner.nextInt();
sum += anInt;
}
System.out.println(sum);
}
}
}
[编程题]A+B(5) 输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。 接下来t行, 每行一组数据。 每行的第一个整数为整数的个数n(1 <= n <= 100)。 接下来n个正整数, 即需要求和的每个正整数。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (scanner.hasNextInt()) {
int n = scanner.nextInt();
int sum = 0;
int count = 0;
while (scanner.hasNextInt() && count < n) {
count++;
sum += scanner.nextInt();
}
System.out.println(sum);
}
}
}
[编程题]A+B(6)
输入数据有多组, 每行表示一组输入数据。 每行的第一个整数为整数的个数n(1 <= n <= 100)。 接下来n个正整数, 即需要求和的每个正整数。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int n = scanner.nextInt();
int sum = 0;
for (int i = 0; i < n; i++) {
sum += scanner.nextInt();
}
System.out.println(sum);
}
}
}
[编程题]A+B(7)
输入数据有多组, 每行表示一组输入数据。 每行不定有n个整数,空格隔开。(1 <= n <= 100)。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
String[] strings = s.split(" ");
int sum = 0;
for (int i = 0; i < strings.length; i++) {
sum += Integer.parseInt(strings[i]);
}
System.out.println(sum);
}
}
}
[编程题]字符串排序(1)
输入有两行,第一行n 第二行是n个空格隔开的字符串
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
String[] strings = s.split(" ");
Arrays.sort(strings);
for (int i = 0; i < strings.length; i++) {
if (i != strings.length - 1) {
System.out.print(strings[i] + " ");
} else {
System.out.print(strings[i]);
}
}
}
}
}
[编程题]字符串排序(2)
多个测试用例,每个测试用例一行。 每行通过空格隔开,有n个字符,n<100
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
String[] strings = s.split(" ");
Arrays.sort(strings);
for (int i = 0; i < strings.length; i++) {
if (i != strings.length - 1) {
System.out.print(strings[i] + " ");
} else {
System.out.println(strings[i]);
}
}
}
}
}
[编程题]字符串排序(3)
多个测试用例,每个测试用例一行。 每行通过,隔开,有n个字符,n<100
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
String[] strings = s.split(",");
Arrays.sort(strings);
for (int i = 0; i < strings.length; i++) {
if (i != strings.length - 1) {
System.out.print(strings[i] + ",");
} else {
System.out.println(strings[i]);
}
}
}
}
}