【java編程(在線筆試)】常見題目

1. 大數相加

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author frlh
 * @create 2020/06/07 15:58
 */
public class Main {

    static Random random = new Random();

    // 核心方法
    public static String sum(String a, String b) {
        int len = Math.max(a.length(), b.length());
        StringBuffer s = new StringBuffer(len + 1);
        int t = 0;
        for (int i = 1; i <= len; i++) {
            t += getLast(a, i) + getLast(b, i);
            s.append(t % 10);
            t /= 10;
        }
        return s.reverse().toString();
    }


    public static int getLast(String a, int last) {
        if (last > a.length()) return 0;
        return a.charAt(a.length() - last) - '0';
    }

    public static String createNum() {
        StringBuffer s = new StringBuffer().append(random.nextInt(9) + 1);
        int len = 100 + random.nextInt(100);
        for (int i = 0; i < len; i++) {
            s.append(random.nextInt(10));
        }
        return s.toString();
    }

    public static void run() {
        List<String> ss = new ArrayList<>(100);
        for (int i = 0; i < 100; i++) {
            ss.add(createNum());
        }
        // System.out.println(ss);

        String s = ss.get(0);
        for (int i = 1; i < ss.size(); i++) {
            s = sum(s, ss.get(i));
        }
        System.out.println(s);
    }


    public static void main(String[] args) {
        run();
        // System.out.println(sum("12", "2"));
        // System.out.println(sum("12", "9"));
        // System.out.println(sum("12", "99"));
    }

}

2. 鏈表的拆分、反轉、合併

/**
 * @author frlh
 * @create 2020/06/07 22:10
 */
public class Main {

    static class Node {
        int value;
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }

    }

    public static Node createNodeList(Integer... args) {
        Node node = null;
        for (int i = 1; i <= args.length; i++) {
            node = new Node(args[args.length - i], node);
        }
        return node;
    }

    public static void printNodeList(Node node) {
        while (node != null) {
            System.out.print(node.value);
            if (node.next != null) {
                System.out.print("->");
            } else {
                System.out.println();
                break;
            }
            node = node.next;
        }
    }

    public static Node cfNodeList(Node a) {
        Node b = a.next;

        Node ta = a, tb = b;
        while (ta.next != null && tb.next != null) {
            ta.next = tb.next;
            ta = ta.next;
            tb.next = ta.next;
            tb = tb.next;
        }
        if (ta != null) ta.next = null; // 容易忽略
        if (tb != null) tb.next = null; // 容易忽略
        return b;
    }

    public static Node cfNodeList1(Node a) {
        if (a == null) return null;
        Node b = a.next;
        a.next = b.next;
        b.next = cfNodeList(b.next);
        return b;
    }

    public static Node fzNodeList(Node a) {

        if (a == null) return null;
        if (a.next == null) return a;

        Node b = a.next;
        if (b.next == null) {
            b.next = a;
            return b;
        }

        Node c = b.next;
        a.next = null; // 容易忽略
        while (c != null) {
            b.next = a;
            a = b;
            b = c;
            c = c.next;
        }
        b.next = a; // 容易忽略
        return b;
    }

    public static Node fzNodeList1(Node a) {

        if (a == null) return null;
        if (a.next == null) return a;
        Node res = fzNodeList(a.next);
        a.next.next = a;
        a.next = null; // 容易忽略

        return res;
    }

    public static Node hbNodeList(Node a, Node b) {
        if (a == null && b == null) {
            return null;
        }

        Node res = new Node(0, null);
        Node tmp = res;

        while (a != null && b != null) {
            if (a.value < b.value) {
                tmp.next = a;
                a = a.next;
            } else {
                tmp.next = b;
                b = b.next;
            }
            tmp = tmp.next;
        }

        if (a == null) {
            tmp.next = b;
        } else {
            tmp.next = a;
        }
        return res.next;
    }

    public static Node hbNodeList1(Node a, Node b) {
        if (a == null && b == null) {
            return null;
        } else if (a == null && b != null) {
            return b;
        } else if (a != null && b == null) {
            return a;
        } else {
            if (a.value < b.value) {
                a.next = hbNodeList(a.next, b);
                return a;
            } else {
                b.next = hbNodeList(a, b.next);
                return b;
            }
        }
    }


    public static void main(String[] args) {

        System.out.println("普通實現:--------------");

        Node a = createNodeList(1, 8, 3, 6, 5, 4, 7);
        printNodeList(a);

        Node b = cfNodeList(a);
        printNodeList(a);
        printNodeList(b);

        Node c = fzNodeList(b);
        printNodeList(c);

        printNodeList(hbNodeList(a, c));


        System.out.println("遞歸實現:--------------");

        Node aa = createNodeList(1, 8, 3, 6, 5, 4, 7);
        printNodeList(aa);

        Node bb = cfNodeList1(aa);
        printNodeList(aa);
        printNodeList(bb);

        Node cc = fzNodeList1(bb);
        printNodeList(cc);

        printNodeList(hbNodeList1(aa, cc));
    }
}

3. 斐波那契數列


public class C {

    public static int f(int n) {
        if (n == 1 || n == 2) return 1;
        return f(n - 1) + f(n - 2);
    }

    public static int f1(int n) {
        Integer[] tmp = new Integer[n + 1];
        return f1_(n, tmp);
    }

    public static int f1_(int n, Integer[] tmp) {
        if (tmp[n] == null) {
            if (n == 1 || n == 2) {
                tmp[n] = 1;
            } else {
                tmp[n] = f1_(n - 1, tmp) + f1_(n - 2, tmp);
            }
        }
        return tmp[n];
    }

    public static int f2(int n) {
        int[] dp = new int[n + 1];
        dp[1] = dp[2] = 1;
        for (int i = 3; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }

    public static int f3(int n) {
        if (n == 1 || n == 2) return 1; // 容易忽略
        int a = 1, b = 1, c = 0;
        for (int i = 3; i <= n; i++) {
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }

    public static void main(String[] args) {
        System.out.println(f(15));
        System.out.println(f1(15));
        System.out.println(f2(15));
        System.out.println(f3(15));
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章