今日頭條在線筆試2018-03-25

1題目描述
在n個(gè)元素的數(shù)組中,找到值為k的數(shù)字對(duì)去重后的個(gè)數(shù)


image.png

image.png
//參考代碼 30% case
public class Main {
    public static void main(String[] args) {
        
    Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
       
        int arr[] = new int[n];
        
        for(int i = 0; i < n; i++){
            arr[i] = sc.nextInt();
        } 
        
        System.out.println(solve(arr,k));
    }
    
    static int solve(int arr[], int k){
        int len = arr.length;
        ArrayList<Integer> rest = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
              if (Math.abs(arr[i] - arr[j]) == k && (!rest.contains(arr[i] + arr[j]))) {
                  rest.add(arr[i] + arr[j]);
              }
            }
          }
        
        return rest.size();
    }
}


2題目描述
打印輸出下面的效果:
輸入描述
第一行是一個(gè)整數(shù)n,接下來(lái)n行都是 由數(shù)字6構(gòu)成的數(shù)學(xué)表達(dá)式(+,-,*法運(yùn)算)
輸出描述
用圖形輸出數(shù)學(xué)表達(dá)式的結(jié)果,如下效果圖


image.png

image.png
//AC 代碼
public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        String arr[] = new String[n];

        for (int i = 0; i < n; i++) {
            arr[i] = sc.next();
        }

        for (int i = 0; i < n; i++) {
            int num = cal(arr[i]);
            String sNum = String.valueOf(num);
            int k = sNum.length();
            // System.out.println(sNum +"--"+k);
            int len = 5 * k + (k - 1) * 2;
            char[][] chars = new char[5][len];
            for (int p = 0; p < 5; p++) {
                for (int q = 0; q < len; q++) {
                    chars[p][q] = '.';
                }
            }

            for (int j = 0; j < k; j++) {
                // 取出字符后減去'0'(注意是字符0)就可以得到數(shù)值了,比如'1'-'0'=1;
                int shu = sNum.charAt(j) - '0';
                switch (shu) {
                case 0:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4) {
                                chars[p][q] = '6';
                            }
                            if (q == 7 * j || q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 1:

                    for (int p = 0; p < 5; p++) {
                        chars[p][7 * j + 4] = '6';
                    }

                    break;
                case 2:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j && p > 1) {
                                chars[p][q] = '6';
                            }

                            if (p < 2 && q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }

                    break;
                case 3:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 4:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j && p < 1) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j + 2) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 5:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j && p < 1) {
                                chars[p][q] = '6';
                            }

                            if (p > 1 && q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 6:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j) {
                                chars[p][q] = '6';
                            }

                            if (p > 1 && q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 7:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 8:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 9:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j && p < 1) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;

                }

            }

            for (int ii = 0; ii < chars.length; ii++) {
                System.out.println(new String(chars[ii]));
            }
        }
    }

    /**
     * 計(jì)算表達(dá)式的值
     * 
     * @param str
     * @return
     */
    static int cal(String str) {

        char[] cs = str.toCharArray();
        int ans = 6;
        for (int i = 1; i < cs.length; i++) {
            switch (cs[i]) {

            case '+':
                ans = ans + 6;
                break;
            case '-':
                ans = ans - 6;
                break;
            case '*':
                ans = ans * 6;
                break;
            case '6':
                break;
            }
        }
        return ans;
    }
}


3題目描述
字符串操作


image.png

image.png


4題目描述


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容