2022.7.1蔚来笔试总结

1年前 (2022) 程序员胖胖胖虎阿
161 0 0

7.1号笔试的,我今天7.4才总结。。。
问题很多,主要还是太久没有用ACM模式,导致自己很被动。其次是脑袋太僵化,有点太紧张了,很多其实动一下脑子就出来了的。
笔试模式是选择+编程
编程一共三道题
怎么说呢,都见过,都不难,但就不是能很快的ac。。。。还是不太熟练
先解决acm模式的问题。

1.ACM模式的输入输出

A.多组空格分割的两个正整数

输入:
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。
输出描述:
输出a+b的结果
输入例子:
1 5
10 20
输出例子:
6
30

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
}

B.第一行组数接空格分隔的两个正整数

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

输出
输出a+b的结果
例子:
2
1 5
10 20
输出
6
30

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
}

C.空格分隔的两个正整数为0 0 结束

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
输出a+b的结果
输入:
1 5
10 20
0 0
输出
6
30

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            if(a ==0 && b == 0) break;
            System.out.println(a + b);
        }
    }
}

D.每行第一个为个数后带空格分割整数为0结束

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

每组数据输出求和的结果
输入
4 1 2 3 4
5 1 2 3 4 5
0
输出
10
15

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            if(n == 0) break;
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += in.nextInt();
            }
            System.out.println(sum);
        }
    }
}

字符串系列

H.第一行个数第二行字符串

输入

输入有两行,第一行n

第二行是n个空格隔开的字符串

输出

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

输入例子

5
c d a bb e

输出例子

a bb c d e
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        while (in.hasNext()) { // 注意 while 处理多个 case
            String[] s = in.nextLine().split(" ");
            Arrays.sort(s);
            for (int i = 0; i < s.length; i++) {
                System.out.print(s[i] + " ");
            }
            
        }
    }
}

懒得总结了 直接上别人的总结
别人的总结
第二个总结

第一题 平均值最大的连续子段

输入5
2 2 2 2 2
输出 2

思路:
一开始总是去想滑动窗口去了
但其实 这个题 先排序 完后直接输出最大值就行了

这里就写一个acm的这个
int n = sc.nextInt();
int[] nums = new int[n];
for(int i=0;i<nums.length;i++){
nums[i] = sc.nextInt();
}

或者用nextLine读取 但要注意 用Line的时候 前面要再加一遍Line

第二题

吃糖问题,一共有n个糖果,第i个糖果的甜度为ni 甜度值总和不能超过k
问最多能吃几个糖果

笨啊!!!
sort之后 从小的开始

第三题 标签匹配

public static void main(String[] args) {
    //System.out.println("YES");
    Deque<String > st=new LinkedList<>();
    Scanner in=new Scanner(System.in);
    String preS = in.nextLine();
    StringBuilder sb=new StringBuilder();
    for (char c : preS.toCharArray()) {
        if(c=='<'){
            sb.append(" ");
        }
        sb.append(c);
        if(c=='>'){
            sb.append(" ");
        }
    }
    boolean flag=true;
    String[] ss = sb.toString().split(" ");
    List<String > temp=new LinkedList<>();
//以上是对字符串的预处理,将字符串划分为三种类型:第一种<abc>   第二种 </abc>    第三种 非第一种和第二种
  for (String s : ss) {
        if(s.equals("")){
            continue;
        }
        temp.add(s);
    }
    ss=temp.toArray(new String[0]);
    for (String s : ss) {
        if(!s.matches("</[a-z]+>")){

        //    System.out.println(s);
            st.push(s);
        }
        if(s.matches("</[a-z]+>")){
            if(st.isEmpty()){
                flag=false;
                break;
            }
            String pop = st.pop();
            if(!pop.matches("<[a-z]+>")){
                if(st.isEmpty()){
                    flag=false;
                    break;
                }
                pop=st.pop();
            }
            if(!pop.substring(1,pop.length()-1).
                    equals(s.substring(2,s.length()-1))){
                flag=false;
                break;
            }
        }
    }
    if(!st.isEmpty()){
        flag=false;
    }
    System.out.println(flag?"YES":"NO");
}

版权声明:程序员胖胖胖虎阿 发表于 2022年11月9日 下午3:24。
转载请注明:2022.7.1蔚来笔试总结 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...