1. 程式人生 > >2018年科大訊飛春招筆試程式設計題

2018年科大訊飛春招筆試程式設計題

科大訊飛2018春招的筆試程式設計題中,第一題是判斷蚊子是否在蚊帳中,比較簡單,這裡不做說明,本文將簡單講解第二題和重點說明第三題的解法。

第二題

1.題目
這裡寫圖片描述

2.輸入輸出及樣例
這裡寫圖片描述

3.思路
這題思路很容易想到,最後拿到的工資可以用m * n - wrongNum * x -k * (rightNum - wrongNum)來計算,其中rightNum是指批改正確的份數,wrongNum是指批改錯誤的份數。但是有一點需要注意的是,當rightNum**小於**wrongNum的時候,不能再用這個公式計算,應該使用m * n - wrongNum * x 來計算。

4.程式碼實現

import java.util.Scanner;

public class Inter2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String[] inputSs = scanner.nextLine().split(" ");
        int n = Integer.parseInt(inputSs[0]);
        int m = Integer.parseInt(inputSs[1]);
        int
x = Integer.parseInt(inputSs[2]); int k = Integer.parseInt(inputSs[3]); int rightNum = 0; int wrongNum = 0; String[] inputSs2 = scanner.nextLine().split(" "); for(int i = 0; i < inputSs2.length; i ++) { if(inputSs2[i].equals("0")) { wrongNum ++; } else
{ rightNum ++; } } if(rightNum >= wrongNum) { System.out.print(m * n - wrongNum * x -k * (rightNum - wrongNum)); } else { System.out.print(m * n - wrongNum * x); } } }
第三題

1.題目
這裡寫圖片描述

2.輸入輸入及樣例
這裡寫圖片描述
這裡寫圖片描述

3.思路
這道題思路不是很清晰,最容易想到的方法應該是將測試板上的字元存入二維陣列中,對於每個操作分別實現一個變換方法。具體見程式碼。

4.程式碼實現
提醒:這個程式碼是筆試結束才完全寫完的,只經過了給出的兩個用例的測試,不保證完全正確。

import java.util.Scanner;

public class Inter3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while(true) {
            int n = Integer.parseInt(scanner.nextLine());
            String[][] input = new String[n][n];
            for(int i = 0; i < n; i ++) {
                char[] inputChars = scanner.nextLine().toCharArray();
                for(int j = 0; j < inputChars.length; j ++) {
                    input[i][j] = String.valueOf(inputChars[j]);
                }
            }

            //獲取指令,並執行相應操作
            String[] orders = scanner.nextLine().split(" ");
            for(String order : orders) {
                switch(order) {
                    case "<":
                        input = turnLeft(input);
                        break;
                    case ">":
                        input = turnRight(input);
                        break;
                    case "|":
                        input = flipVertical(input);
                        break;
                    case "-":
                        input = flipHorizontal(input);
                        break;
                    case "\\":
                        input = turnDiagonalt(input);
                        break;
                    case "/":
                        input = turnAntiDiagonalt(input);
                }
            }

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    System.out.print(input[i][j]);
                }
                System.out.println();
            }
        }

    }

    //向左旋轉90度
    public static String[][] turnLeft(String[][] input) {
        int length = input.length;
        String[][] temp = new String[length][length];

        String oldChar = "";
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                oldChar = input[j][length - 1 - i];
                if(oldChar.equals("<")) {
                    temp[i][j] = "v";
                } else if(oldChar.equals(">")) {
                    temp[i][j] = "^";
                } else if(oldChar.equals("^")) {
                    temp[i][j] = "<";
                } else if(oldChar.equals("v")) {
                    temp[i][j] = ">";
                } else if(oldChar.equals("|")) {
                    temp[i][j] = "-";
                } else if(oldChar.equals("-")) {
                    temp[i][j] = "|";
                }else if(oldChar.equals("\\")) {
                    temp[i][j] = "/";
                } else if(oldChar.equals("/")) {
                    temp[i][j] = "\\";
                } else {
                    temp[i][j] = oldChar;
                }
            }
        }
        return temp;
    }

    //向右旋轉90度
    public static String[][] turnRight(String[][] input) {
        int length = input.length;
        String[][] temp = new String[length][length];

        String oldChar = "";
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                oldChar = input[length - 1 - j][i];
                if(oldChar.equals("<")) {
                    temp[i][j] = "^";
                } else if(oldChar.equals(">")) {
                    temp[i][j] = "v";
                } else if(oldChar.equals("^")) {
                    temp[i][j] = ">";
                } else if(oldChar.equals("v")) {
                    temp[i][j] = "<";
                } else if(oldChar.equals("|")) {
                    temp[i][j] = "-";
                } else if(oldChar.equals("-")) {
                    temp[i][j] = "|";
                }else if(oldChar.equals("\\")) {
                    temp[i][j] = "/";
                } else if(oldChar.equals("/")) {
                    temp[i][j] = "\\";
                } else {
                    temp[i][j] = oldChar;
                }
            }
        }
        return temp;
    }

    //沿水平方向翻轉
    public static String[][] flipHorizontal(String[][] input) {
        int length = input.length;
        String[][] temp = new String[length][length];

        String oldChar = "";
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                oldChar = input[length - 1 - i][j];

                if(oldChar.equals("^")) {
                    temp[i][j] = "v";
                } else if(oldChar.equals("v")) {
                    temp[i][j] = "^";
                }else if(oldChar.equals("\\")) {
                    temp[i][j] = "/";
                } else if(oldChar.equals("/")) {
                    temp[i][j] = "\\";
                } else {
                    temp[i][j] = oldChar;
                }
            }
        }

        return temp;
    }

    //沿垂直方向翻轉
    public static String[][] flipVertical(String[][] input) {
        int length = input.length;
        String[][] temp = new String[length][length];

        String oldChar = "";
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                oldChar = input[i][length - 1 - j];
                if(oldChar.equals(">")) {
                    temp[i][j] = "<";
                } else if(oldChar.equals("<")) {
                    temp[i][j] = ">";
                } else if(oldChar.equals("\\")) {
                    temp[i][j] = "/";
                } else if(oldChar.equals("/")) {
                    temp[i][j] = "\\";
                } else {
                    temp[i][j] = oldChar;
                }
            }
        }

        return temp;
    }


    //沿對角線翻轉
    public static String[][] turnDiagonalt(String[][] input) {
        int length = input.length;
        String[][] temp = new String[length][length];

        String oldChar = "";
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                oldChar = input[j][i];
                if(oldChar.equals("<")) {
                    temp[i][j] = "^";
                } else if(oldChar.equals(">")) {
                    temp[i][j] = "v";
                } else if(oldChar.equals("^")) {
                    temp[i][j] = "<";
                } else if(oldChar.equals("v")) {
                    temp[i][j] = ">";
                } else if(oldChar.equals("|")) {
                    temp[i][j] = "-";
                } else if(oldChar.equals("-")) {
                    temp[i][j] = "|";
                } else {
                    temp[i][j] = oldChar;
                }
            }
        }
        return temp;
    }

    //沿反對角線翻轉
    public static String[][] turnAntiDiagonalt(String[][] input) {
        int length = input.length;
        String[][] temp = new String[length][length];

        String oldChar = "";
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                oldChar = input[length - 1 - j][length - 1 - i];
                if(oldChar.equals("<")) {
                    temp[i][j] = "v";
                } else if(oldChar.equals(">")) {
                    temp[i][j] = "^";
                } else if(oldChar.equals("^")) {
                    temp[i][j] = ">";
                } else if(oldChar.equals("v")) {
                    temp[i][j] = "<";
                } else if(oldChar.equals("|")) {
                    temp[i][j] = "-";
                } else if(oldChar.equals("-")) {
                    temp[i][j] = "|";
                } else {
                    temp[i][j] = oldChar;
                }
            }
        }
        return temp;
    }

}
小結

本人演算法方面薄弱,這裡給出的解法可能不是最佳,特別是第三道題,這樣實現太過麻煩,而且很冗長。還望有更好解法的大神指點,不勝感激!