一些代码笔试题和易错题
   一些代码   3 评论   2407 浏览

一些代码笔试题和易错题

   一些代码   3 评论   2407 浏览

美团:ab串

/**
 * 有多少个长度为n的仅由a和b组成的字符串既不包含aba也不包含bab?
 * 最后模上998244353
 *
 * 输入描述
 * 第一行有一个整数n(1<=n<=100000),代表小美问题中的参数。
 *
 * 输出描述
 * 输出一个整数,即小美所问问题的答案除以998244353所得余数。
 *
 * n = 1 r = 2
 * n = 2 r = 4
 * n = 3 r = 6
 * n = 4 r = 10
 *
 * @author RawChen
 * @date 2021/9/16 23:41
 */
public static void main(String[] args) {
    ArrayList<String> l = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    for (int i = 0; i < Math.pow(2, n); i++) {
        StringBuilder t = new StringBuilder(Integer.toBinaryString(i));
        if (t.length() < n) {
            //如果所遍历的数字转为2进制字符串时位数不够则补0直到够n位
            while (t.length() != n) {
                t.insert(0, "0");
            }
        }
        l.add(t.toString());
    }
    int result = 0;
    for (String o : l) {
        // System.out.println(o);
        if (!o.contains("010") && !o.contains("101")) {
            result++;
        }
    }
    System.out.println(result % 998244353);
}

度小满:小A的节奏治疗

/**
 * 小A的节奏治疗
 *
 * 在xxxx年,小A成功研发出了一种新型疗法:节奏疗法。
 * 节奏疗法是这样的:使用心跳节奏器来识别病人的心跳是否稳定,并在合适的时候按下节奏器上的按钮,
 * 来调节引导病人的心跳保持稳定。但是,在不合适的地方按下时,则可能会导致病情恶化。
 * 于是,医生在治疗的时候需要时刻保持专注。
 * 在某一段时间内,病人的心跳节奏可以用OX来表示,其中O表示此时可以按下按钮或者不按,而X表示此时不能按下按钮。
 * 为了训练,小A找来m位医生,并给他们一段模拟的心跳节奏,之后小A得到了这m位医生各自的按键情况。
 * 小A找你帮忙写个程序,来判断哪些医生的按键是合法的。
 *
 * 输入描述
 * 第一行为只由OX组成的字符串,表示模拟的心跳节奏;
 * 第二行一个正整数m;
 * 接下来m行,每行一个只有0、1组成的字符串,表示该医生的按键情况。其中0表示此时未按键,1表示此时有按键。
 * 设表示心跳节奏的长度。
 * ,所有的字符串长度相同。
 *
 * 输出描述
 * 输出m行。如果该医生的按键情况合法,那么输出一行“YES”(不含引号),否则输出“NO”。
 *
 * 样例输入
 * OOXX
 * 3
 * 1111
 * 1100
 * 0100
 *
 * 样例输出
 * NO
 * YES
 * YES
 *
 * @author RawChen
 * @date 2021/9/5 16:22
 */
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    String arr;
    arr = s.nextLine();
    int m = s.nextInt();
    String t[] = new String[m];
    for (int i = 0; i < m; i++) {
        t[i] = s.next();
    }
    for (String s1 : t) {
        boolean flag = true;
        for (int i = 0; i < arr.length(); i++) {
            //如果原始i位心跳为X且输入对应的i位为1,则说明不合法
            if (("X".equals(String.valueOf(arr.charAt(i)))) 
                    && ("1".equals(String.valueOf(s1.charAt(i))))) {
                flag = false;
                break;
            }
        }
        if (flag == true) {
            System.out.println("YES");
        }else {
            System.out.println("NO");
        }
    }
}

StringBuffer作方法参数

public static void main(String[] args) {
    StringBuffer a = new StringBuffer("A");
    StringBuffer b = new StringBuffer("B");
    // 取代 oper(a,b) start;
    StringBuffer x;
    StringBuffer y;
    x = a;            // x的地址指向了a,此时 x 的值为"A"
    y = b;            // y的地址指向了b,此时 y 的值为"B"
    x.append("B");    // 在x(a)的地址值变为"AB"
    y = x;             // y的地址指向了x,也就是指向了a,这个过程中根本就没对b做任何操作
    // 取代 oper(a,b) end;
    System.out.print(a + "," + b); //结果:AB,B
}
static void oper(StringBuffer x, StringBuffer y) {
    x.append("B");
    y = x;
    // 一开始的想法是这样的,x = a; y= b; 那么 x.append("B");后 x="AB",y=x="AB"
    // 实际上并不是数学中的简单等价传递

代码块

代码块编译期错误:非法前向引用

public class CodeBlock {
    {
        S += 3;
    }

    private int M = 1;
    private int S = M + 1;
    public CodeBlock(int var) {
        System.out.println(M);
        System.out.println(S);
        this.M = var;
        System.out.println(M++);
        System.out.println(S);
    }
    public static void main(String[] args) {
        new CodeBlock(6);
    }
}

异常捕捉测试

f()返回结果为3,执行try后无异常,再执行finally,再执行return

public static void main(String[] args) {
    System.out.println(f());
}
public static int f() {
    int i = 0;
    try {
        System.out.println("try: " + (++i));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        System.out.println("finally: " + (++i));
    }
    return ++i;
}

执行顺序

public class ExecutionOrder {
    public static void main(String[] args) {
        Father father = new Son();
        father.action();
        System.out.println("----------------");
        father = new Son();
    }
}

class Father {
    {
        System.out.println("F 普通代码块");
    }
    static{
        System.out.println("F 静态代码块");
    }
    public Father() {
        System.out.println("F 构造");
    }
    public void action() {
        System.out.println("F action");
    }
}

class Son extends Father{
    {
        System.out.println("S 普通代码块");
    }
    static{
        System.out.println("S 静态代码块");
    }
    public Son() {
        System.out.println("S 构造");
    }
    public void action() {
        System.out.println("S action");
    }
}
F 静态代码块
S 静态代码块
F 普通代码块
F 构造
S 普通代码块
S 构造
S action
----------------
F 普通代码块
F 构造
S 普通代码块
S 构造

ArrayList的remove问题

public static void main(String[] args) {
    ArrayList<Integer> arr = new ArrayList<>();
    for (int i = 10; i < 15; i++) {
        arr.add(i);
    }
    // arr.remove(11);
    arr.remove((Integer) 11);
    for (Object o : arr) {
        System.out.println(o);
    }
}

因为remove移除后arr.size()的大小不对了,get不到越界的下标

public static void main(String[] args) {
    ArrayList<Integer> arr = new ArrayList<>();
    for (int i = 10; i < 15; i++) {
        arr.add(i);
    }
    for (int i = 0; i <= arr.size(); i++) {
        if (arr.get(i).equals(11)) {
            arr.remove(i);
        }
    }
}

解决办法使用迭代器,或者remove后i--

Iterator it = arr.iterator();
while (it.hasNext()) {
    if (it.next().equals(11)) {
        it.remove();
    }
}
for (Integer i : arr) {
    System.out.println(i);
}

本文由 RawChen 发表, 最后编辑时间为:2021-12-22 22:23
如果你觉得我的文章不错,不妨鼓励我继续写作。

发表评论
选择表情
  1. 邹杰

    第一道题需要掌握补码嘛?

       Windows 10   Chrome 96
    1. RawChen 博主
      @邹杰

      不用的,只是看它组合是aba只包含a和b,这种组合很容易就有很简单的解法就是当做二进制来解 icon_rolleyes.png

         Windows 10   Chrome 94
  2. 网站每日ip 1千,交换友链,https://money1.us/521

       Windows 7   Chrome 91
Top