牛客竞赛练习

竞赛网址:https://ac.nowcoder.com/acm/contest/5652#question

1.

 

输入描述:输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。
输出描述:输出a+b的结果
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         while (sc.hasNextInt()){
 7             Integer a = sc.nextInt();
 8             Integer b = sc.nextInt();
 9             System.out.println(a+b);
10         }
11     }
12 }

整个执行过程:

如果输入 1 2 3 4,在第一个hasNextInt()扫描到1后返回true,进入循环,出来循环后,第二个hasNextInt()扫描到的是3了,也返回true,所以结果是

 

3
7

 

Scanner知识扩展:

当执行到hasNext()时,它会先扫描缓冲区中是否有字符,有则返回true,继续扫描。直到扫描为空,这时并不返回false,而是将方法阻塞,等待你输入内容然后继续扫描。

你可以设置一个终止符,调用hasNext()的重载方法hasNext(String patten):如果下一个标记与从指定字符串构造的模式匹配,则返回 true。匹配#返回true,然后取非运算。即以#为结束符号。

2.

输入描述:
输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)

输出描述:
输出a+b的结果
import java.util.Scanner;

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

3.

输入描述:

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


输出描述:
输出a+b的结果
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         while (sc.hasNextInt()){
 7             int a = sc.nextInt();
 8             int b = sc.nextInt();
 9             if (a==0&b==0){
10                 return;
11             }
12             System.out.println(a+b);
13         }
14     }
15 }

4.

输入描述:

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

输出描述:
每组数据输出求和的结果
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         int j;
 7         while (sc.hasNextInt()){
 8             int n = sc.nextInt();
 9             if (n==0){
10                 return;
11             }else {
12                 j=0;
13                 for (int i=0;i<n;i++){
14                     j = j+sc.nextInt();
15                 }
16                 System.out.println(j);
17             }
18         }
19     }
20 }

 5.

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

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         int s;
 7         int n = sc.nextInt();
 8         for (int i = 0; i <n ; i++) {
 9             while (sc.hasNextInt()){
10                 s=0;
11                 int m = sc.nextInt();
12                 for (int j = 0; j <m ; j++) {
13                     s = s+sc.nextInt();
14                 }
15                 System.out.println(s);
16             }
17         }
18     }
19 }

6.

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

import java.util.Scanner;

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

7.

输入描述:
输入数据有多组, 每行表示一组输入数据。

每行不定有n个整数,空格隔开。(1 <= n <= 100)。
输出描述:
每组数据输出求和的结果

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         int j;
 7         while (sc.hasNextLine()){
 8             String line = sc.nextLine();
 9             String[] strings = line.split("\\s+");
10             j = 0;
11             for(String n:strings){
12                 int i = Integer.parseInt(n);
13                 j = j+i;
14             }
15             System.out.println(j);
16         }
17 
18     }
19 }

Scanner和String知识扩展:

参考:https://www.cnblogs.com/mingforyou/archive/2013/09/03/3299569.html

https://www.cnblogs.com/xiaoxiaohui2015/p/5838674.html

https://www.cnblogs.com/cocobear9/p/12708057.html

https://blog.csdn.net/xueqinmax/article/details/81748034

1.hasNextLine():如果在此扫描器的输入中存在另一行,则返回 true。

2.

在java.lang包中有String.split()方法,返回是一个数组
我在应用中用到一些,给大家总结一下,仅供大家参考:
1、如果用“.”作为分隔的话,必须是如下写法,String.split("\\."),这样才能正确的分隔开,不能用String.split(".");
2、如果用“|”作为分隔的话,必须是如下写法,String.split("\\|"),这样才能正确的分隔开,不能用String.split("|");
“.”和“|”都是转义字符,必须得加"\\";
3、如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如,“acount=? and uu =? or n=?”,把三个都分隔出来,可以用String.split("and|or");

然后就要明确正则表达式的含义了:
\\s表示 空格,回车,换行等空白符,
+号表示一个或多个的意思,所以...

3.在 Java 中要将 String 类型转化为 int 类型时,需要使用 Integer 类中的 parseInt() 方法

 8.

输入描述:
输入有两行,第一行n

第二行是n个空格隔开的字符串
输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格

 1 import java.util.Arrays;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     public static void main(String[] args) {
 6         Scanner sc = new Scanner(System.in);
 7         int n = sc.nextInt();
 8         String[] s = new String[n];
 9         if (sc.hasNextLine()){
10             sc.nextLine();        //这里需要注意一下,https://blog.csdn.net/superme_yong/article/details/80543995,next()后面有nextLine()时一定要在中间再加一个nextLine()
11             String line = sc.nextLine();
12             String[] split = line.split(" ");
13             for (int i = 0; i < n ; i++) {
14                 s[i] = split[i];
15             }
16             Arrays.sort(s);
17             System.out.println(String.join(" ",s));
18         }
19     }
20 }

 

posted @ 2020-12-21 23:34  jacknoe  阅读(326)  评论(0)    收藏  举报