牛客机考算法题输入输出case。

1、A+B(1)

输入描述:

输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。

输出描述:

输出a+b的结果

示例1

输入:

1 5
10 20

输出:

6

30

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}
}
}

2、A+B(2)

输入描述:

1
2
输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 1000)

输出描述:

输出a+b的结果

示例1

输入:

2

1 5

10 20

输出:

6

30

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 n=in.nextInt();
while (in.hasNextInt()) { // 注意 while 处理多个 case
for(int i=0;i<n;i++){
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
}

3、A+B(3)

输入描述:

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入

输出描述:

输出a+b的结果

示例

输入:

1 5

10 20

0 0

输出:

6

30

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
}
}
}

4、A+B(4)

输入描述:

1
2
3
输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。

输出描述:

每组数据输出求和的结果

示例1

输入:

4 1 2 3 4

5 1 2 3 4 5 0

输出:

10

15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 sum=0;
while (in.hasNextLine()) { // 注意 while 处理多个 case
int n=in.nextInt();
if(n==0){
break;
}
for(int i=0;i<n;i++){
sum+=in.nextInt();
}
System.out.println(sum);
sum=0;
}
}
}

5、A+B(5)

输入描述:

1
2
3
4
输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

输出描述:

每组数据输出求和的结果

示例1

输入例子:

2

4 1 2 3 4

5 1 2 3 4 5

输出例子:

10

15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int row=in.nextInt();
int count=0;
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
int sum=0;
int size=in.nextInt();
for(int i=0;i<size;i++){
sum+=in.nextInt();
}
System.out.println(sum);
count++;
if(count==row){
break;
}
}
}
}

6、A+B(6)

输入描述:

1
2
3
输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

输出描述:

每组数据输出求和的结果

示例1

输入例子:

4 1 2 3 4

5 1 2 3 4 5

输出例子:

10

15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 size=in.nextInt();
int sum=0;
for(int i=0;i<size;i++){
sum+=in.nextInt();
}
System.out.println(sum);
}
}
}

7、A+B(7)

输入描述:

1
2
3
输入数据有多组, 每行表示一组输入数据。

每行不定有n个整数,空格隔开。(1 <= n <= 100)。

输出描述:

每组数据输出求和的结果

示例1

输入例子:

1 2 3

4 5

0 0 0 0 0

输出例子:

6

9

0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 sum=0;
String[] str=in.nextLine().split(" ");
for(int n =0;n<str.length;n++){
sum += Integer.parseInt(str[n]);
}
System.out.println(sum);
}
}
}

8、字符串排序(1)

对输入的字符串进行排序后输出

输入描述:

1
2
3
输入有两行,第一行n

第二行是n个字符串,字符串之间用空格隔开

输出描述:

输出一行排序后的字符串,空格隔开,无结尾空格

示例1

输入例子:

5
c d a bb e

输出例子:

a bb c d e

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 sc = new Scanner(System.in);
int countWord = sc.nextInt();
while(sc.hasNextLine()){
String[] str = sc.nextLine().split(" ");
Arrays.sort(str);
StringBuffer sb = new StringBuffer();
for(String s:str){
sb.append(s).append(" ");
}
System.out.print(sb.substring(0,sb.length()-1));
}
}
}

9、字符串排序(2)

对输入的字符串进行排序后输出

输入描述:

1
2
3
多个测试用例,每个测试用例一行。

每行通过空格隔开,有n个字符,n<100

输出描述:

对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开

示例1

输入例子:

a c bb
f dddd
nowcoder

输出例子:

a bb c

dddd f

nowcoder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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().split(" ");
Arrays.sort(str);
StringBuffer sb = new StringBuffer();
for(String s:str){
sb.append(s).append(" ");
}
System.out.println(sb.substring(0,sb.length()-1));
}
}

10、字符串排序(3)

对输入的字符串进行排序后输出

输入描述:

1
2
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100

输出描述:

对于每组用例输出一行排序后的字符串,用’,’隔开,无结尾空格

示例1

输入例子:

a,c,bb

f,dddd

nowcoder

输出例子:

a,bb,c

dddd,f

nowcoder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String[] arrs = sc.nextLine().split(",");
Arrays.sort(arrs);
for(int i =0;i<arrs.length;i++){
if(i == arrs.length -1){
System.out.print(arrs[i]);
}else{
System.out.print(arrs[i]+",");
}
}
System.out.println();
}
}
}


本站总访问量