牛客-OJ在线编程常见输入输出练习-Java版
练习地址:https://www.nowcoder.com/exam/test/92918010/detail?pid=27976983#question
1.只有输出
输出一个字符串 Hello Nowcoder!
输入例子:
无
输出例子:
Hello Nowcoder!
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
System.out.println("Hello Nowcoder!");
}
}
2.单组_A+B
给定两个整数 a 和 b ,请你求出 a+b 的值。
输入例子:
1 2
输出例子:
3
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
3.多组_A+B_EOF形式
给定若干组测试数据,读取至文件末尾为止。
每组数据有两个整数 a 和 b ,请你求出 a+b 的值。
输入例子:
1 2
114 514
2024 727
输出例子:
3
628
2751
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
in.nextLine();
}
}
}
4.多组_A+B_T组形式
给定 t 组测试数据。
每组数据有两个整数a和b,请你求出 a+b 的值。
输入例子:
3
1 2
114 514
2024 727
输出例子:
3
628
2751
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long t = in.nextInt();
int i = 0;
// 注意 hasNext 和 hasNextLine 的区别
while (i<t) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
i++;
}
}
}
5.多组_A+B_零尾模式
给定若干组测试数据,最后一组数据为 0 0 ,作为输入的结尾。
每组数据有两个整数 a 和 b ,请你求出 a+b 的值。
输入例子:
1 2
114 514
2024 727
0 0
输出例子:
3
628
2751
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
if(a==0 && b==0) {
break;
}
System.out.println(a + b);
}
}
}
6.单组_一维数组
给定一个长度为 n 的正整数数组 a ,第 i 个元素的值为 a i。
请你求出数组元素之和。
输入例子:
3
1 4 7
输出例子:
12
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextInt();
long sum = 0;
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
sum = sum + in.nextInt();
}
System.out.println(sum);
}
}
7.多组_一维数组_T组形式
给定 t 组询问,每次询问给出一个长度为 n 的正整数数组 a ,第 i 个元素的值为 a i。
请你分别求出每个数组的元素之和。
输入例子:
3
3
1 4 7
1
1000
2
1 2
输出例子:
12
1000
3
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long t = in.nextInt();
long i = 0;
// 注意 hasNext 和 hasNextLine 的区别
while (i < t) { // 注意 while 处理多个 case
long n = in.nextInt();
long j = 0;
long sum = 0;
while (j < n) {
sum = sum + in.nextInt();
j++;
}
System.out.println(sum);
i++;
}
}
}
8.单组_二维数组
给定一个 n 行 m 列的二维正整数数组
请计算数组中所有元素之和。
输入例子:
3 4
1 2 3 4
5 6 7 8
9 10 11 12
输出例子:
78
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
// 注意 hasNext 和 hasNextLine 的区别
long sum = 0;
while (in.hasNextInt()) { // 注意 while 处理多个 case
sum = sum + in.nextInt();
}
System.out.println(sum);
}
}
9.多组_二维数组_T组形式
给定 t 组询问,每次询问给出一个 n 行 m 列的二维正整数数组 a
请你分别求出每个数组的元素之和。
输入例子:
3
3 4
1 2 3 4
5 6 7 8
9 10 11 12
1 1
2024
3 2
1 1
4 5
1 4
输出例子:
78
2024
16
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long t = in.nextInt();
long i = 0;
// 注意 hasNext 和 hasNextLine 的区别
while (i < t) { // 注意 while 处理多个 case
long a = in.nextInt();
long b = in.nextInt();
long total = a * b;
long j = 0;
long sum = 0;
while(j<total) {
sum = sum + in.nextInt();
j++;
}
System.out.println(sum);
i++;
}
}
}
10.单组_字符串
给定一个长度为 n 的字符串 s ,请你将其倒置,然后输出。
输入例子:
5
abcde
输出例子:
edcba
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
in.nextLine();
String str = in.nextLine();
String newStr = "";
for (int i = str.length(); i > 0; i--) {
newStr = newStr.concat(String.valueOf(str.charAt(i - 1)));
}
System.out.println(newStr);
}
}
11.多组_字符串_T组形式
给定t 组询问,每次给出一个长度为 n 的字符串 s ,请你将其倒置,然后输出。
输入例子:
3
5
abcde
8
redocwon
9
tfarcenim
输出例子:
edcba
nowcoder
minecraft
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long t = in.nextInt();
long i = 0;
// 注意 hasNext 和 hasNextLine 的区别
while (i < t) { // 注意 while 处理多个 case
int n = in.nextInt();
in.nextLine();
String str = in.nextLine();
String newStr = "";
for (int j = n; j > 0; j--) {
newStr = newStr.concat(String.valueOf(str.charAt(j - 1)));
}
System.out.println(newStr);
i++;
}
}
}
12.单组_二维字符数组
给定一个 n 行 m 列的二维字符数组 a
请你对行和列都倒置,然后输出之。
输入例子:
3 4
abcd
efgh
ijkl
输出例子:
lkji
hgfe
dcba
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
int n = in.nextInt();
in.nextLine();
String[] res = new String[t];
int i = 0;
while (i < t) {
String str = in.nextLine();
String newStr = "";
for (int j = n; j > 0; j--) {
newStr = newStr.concat(String.valueOf(str.charAt(j - 1)));
}
res[i] = newStr;
i++;
}
for (int k = t; k > 0; k--) {
System.out.println(res[k - 1]);
}
}
}
13.多组_带空格的字符串_T组形式
给定 t 组询问,每次给出一个长度为 n 的带空格的字符串 s ,请你去掉空格之后,将其倒置,然后输出。
输入例子:
3
9
one space
11
two spaces
14
three spaces
输出例子:
ecapseno
secapsowt
secapseerht
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
int i = 0;
while (i < t) {
int n = in.nextInt();
in.nextLine();
String str = in.nextLine();
String newStr = "";
for (int j = n; j > 0; j--) {
if(" ".equals(String.valueOf(str.charAt(j - 1)))) {
continue;
}
newStr = newStr.concat(String.valueOf(str.charAt(j - 1)));
}
System.out.println(newStr);
i++;
}
}
}
14.单组_保留小数位数
给定一个小数n ,请你保留 3位小数后输出。
如果原来的小数位数少于 3 ,需要补充 0 。
如果原来的小数位数多于 3 ,需要四舍五入到 3 位。
输入例子:
1.23
输出例子:
1.230
输入例子:
114.514
输出例子:
114.514
输入例子:
123
输出例子:
123.000
import java.math.*;
import java.text.DecimalFormat;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Double s = in.nextDouble();
// System.out.println(String.format("%.3f", s));
BigDecimal num = in.nextBigDecimal();
DecimalFormat df = new DecimalFormat("0.000");
System.out.println(df.format(num));
}
}
15.单组_补充前导零
给定一个正整数 n ,请你保留 9 个数位,然后输出。
如果数位少于 9 个,那么需要补充前导零。
输入例子:
123
输出例子:
000000123
输入例子:
123456789
输出例子:
123456789
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigDecimal num = in.nextBigDecimal();
DecimalFormat df = new DecimalFormat("000000000");
System.out.println(df.format(num));
}
}
16.单组_spj判断YES与NO
给定一个正整数 n 。
如果 n 是奇数,输出 YES 。
如果n 是偶数,输出 NO 。
输入例子:
123
输出例子:
YES
输入例子:
124
输出例子:
no
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int num = in.nextInt();
if(num % 2 == 0) {
System.out.println("NO");
} else {
System.out.println("YES");
}
}
}
17.单组_spj判断浮点误差
给定一个圆的半径 r ,请你求出该圆的面积。
输入例子:
123
输出例子:
47529.155256
import java.math.*;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
Double res = num * num * Math.PI;
System.out.println(String.format("%.6f", res));
}
}
18.单组_spj判断数组之和
给定两个整数n 和 m ,请你构造一个长度为 n 的正整数数组,使得其元素之和为m 。
输入例子:
3 6
输出例子:
1 2 3
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in .nextInt();
int m = in.nextInt();
for (int i=1;i<=n;i++) {
if (i == n) {
System.out.println(m);
break;
}
System.out.print(1);
System.out.print(" ");
m = m -1;
}
}
}
1611

被折叠的 条评论
为什么被折叠?



