2020年3月7日 星期六

[zerojudge]a054. 電話客服中心

a054. 電話客服中心

根據輸入提供的身分證後9碼,去列出符合規範的首位字母碼。
想到之前「a020. 身分證檢驗 」寫過的程式,複製貼上變成一個方法,檢查該身分證字號是否有效。
main裡面只要簡單的for迴圈讓A~Z跑過一遍,符合的列印出來就好,太輕鬆了吧!但是效率並沒有反向推算來的高喔,有興趣的可以試試看。

程式碼如下:

/* a054. 電話客服中心
*
*
* 2020/3/7
*/

import java.util.Scanner;

public class Pa054{

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

        while(scanner.hasNext()){
            String s = scanner.nextLine();

            for(int i = 'A'; i <= 'Z'; i++){
                System.out.print(isValidid((char)i + s)? (char)i: "");
            }

            System.out.println();
        }
    }

    public static boolean isValidid(String input){
        String[] idNum = {"10", "11", "12", "13", "14", "15", "16", "17", "34",
            "18", "19", "20", "21", "22", "35", "23", "24", "25",
            "26", "27", "28", "29", "32", "30", "31", "33"};  

        int[] num = {1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1};

        input = idNum[input.charAt(0) - 'A'] + input.substring(1);

        int sum = 0;

        for(int i = 0; i < input.length(); i++){
            sum += num[i] * Character.getNumericValue(input.charAt(i));
        }

        return sum % 10 == 0; 
    }
}    

2020年3月6日 星期五

[zerojudge]a053. Sagit's 計分程式

a053. Sagit's 計分程式

答對的題數越多單題得就越低,可以透過巢狀if、for迴圈、多重if之類的來達成,方法很多樣,我使用的是多重if,算式可以再進一步簡化。

想說一下關於&&和&的差別,&是只要一個是False就回傳False了,&&是即便第一個就是False,也會去執行判斷第二個項目。
如果判斷是這樣:a == b & a++ == c,那如果a != b 那 a 就不會+1了。若是寫成 a == b && a++ == c 那不論 a 是否等於 b ,到最後 a 都會+1

T F
T T F
F F F

程式碼如下:

/* a053. Sagit's 計分程式 
*
* 2020/3/7
*/

import java.util.Scanner;

public class Pa053{

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

        while(scanner.hasNext()){
            int n = scanner.nextInt();
            int score = 0;

            if(isBetween(0, n, 10)){
                score += n * 6;
            }else if(isBetween(11, n, 20)){
                score += 10 * 6;
                score += (n - 10) * 2;
            }else if(isBetween(21, n, 40)){
                score += 10 * 6;
                score += 10 * 2;
                score += (n - 20) * 1;
            }else{
                score = 100;
            } 

            System.out.println(score);
        }
    }

    public static boolean isBetween(int min, int v, int max){
        return min <= v & v <= max;
    }
}      

2020年3月5日 星期四

[zerojudge]a044. 空間切割

a044. 空間切割

圖1 圖2
參考影片:(DM10-20131118-03) 平面分割空間

沒有用lineCut和spaceCut是因為會超時TLE(Time Limit Exceed)

程式碼如下:

/* a044. 空間切割  
*
* 2020/3/5
*/

import java.util.Scanner;

public class Pa044{

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

        while(scanner.hasNext()){
            System.out.println(spaceCutSuper(scanner.nextInt()));
        }
    }

    public static int lineCut(int n){
        return n == 0? 1: lineCut(n - 1) + n;
    }

    public static int spaceCut(int n){
        return n == 0? 1: spaceCut(n - 1) + lineCut(n - 1);
    }

    public static int spaceCutSuper(int n){
        return (n * n * n + 5 * n + 6 ) / 6;
    }
}   

2020年3月4日 星期三

[zerojudge]a042. 平面圓形切割

a042. 平面圓形切割

平面圓形切割

    a0 = 1
    a1 = 2
    a2 = 4
    a3 = 8
    a4 = 14
    a5 = 22
    
an - an-1 = 2 * (n - 1), if n > 1
=> an = 2 * (n - 1) + an-1

寫成遞迴就OK了,也可以寫成公式
推導
an = n2 - n + 2, if n > 0

程式碼如下:

/* a042. 平面圓形切割  
*
* 2020/3/3
*/

import java.util.Scanner;

public class Pa042{

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

        while(scanner.hasNext()){
            System.out.println(circleCut(scanner.nextInt())); 
        }
    }

    public static int circleCut(int n){
        return n == 1? 2: 2 * (n - 1) + circleCut(n - 1);
    }

}   

2020年3月2日 星期一

[zerojudge]a040. 阿姆斯壯數

a040. 阿姆斯壯數

阿姆斯壯數有好多名字,我聽過的是水仙花數
其特徵就是長度n位的數,各位數的n次方總和等於自身,例如115 = 1^3 + 1^3 + 5^3

解題想法:
寫一個方法(method)來判斷一數是否為水仙花數,是回傳true,不是就回傳false。
方法內容是先將整數換成字串再換成字元陣列方便對各位數計算,for迴圈之中使用之前用過得Character.getNumericValue。

程式碼如下:

/* a040. 阿姆斯壯數  
*
* 2020/3/2
*/

import java.util.Scanner;

public class Pa040{

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

        while(scanner.hasNext()){
            int n = scanner.nextInt();
            int m = scanner.nextInt();

            boolean none = true;

            for(; n <= m; n++){
                if(isNarcissistic(n)){
                    System.out.print(n + " ");
                    none = false;
                }
            }

            System.out.println(none? "none": "");
        }
    }

    public static boolean isNarcissistic(int num){
        char[] nums = Integer.toString(num).toCharArray();

        int sum = 0;

        for(char ch: nums){
            sum += (int)Math.pow(Character.getNumericValue(ch), nums.length);
        }

        return num == sum;
    }
}   

[zerojudge]a038. 數字翻轉

a038. 數字翻轉

1234 → 4321,1000 → 0001 → 1,000 → 0。
之前迴文用過的StringBuffer加上Integer.parseInt()就完成了。

程式碼如下:

/* a038. 數字翻轉
*
* 2020/3/2
*/

import java.util.Scanner;

public class Pa038{

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

        while(scanner.hasNext()){
            StringBuffer sb = new StringBuffer(scanner.next());

            System.out.println(Integer.parseInt(sb.reverse().toString())); 
        }
    }
}   

2020年3月1日 星期日

[zerojudge]a034. 二進位制轉換

a034. 二進位制轉換

日常生活中使用的是十進位,遇到十就進位,那二進位就是遇到二就進位。 十進位轉二進位 可以參考:【CodingBar】什麼是二進位?|程人式界科普 #01

方法一—直接toBinaryString,超簡單暴力。
方法二—樸實除除樂,如上圖去取除二之後的餘數,然後把順序倒過來輸出。

方法一:

/* a034. 二進位制轉換
*
* 2020/3/2
*/

import java.util.Scanner;

public class Pa034{

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

        while(scanner.hasNext()){
            System.out.println(Integer.toBinaryString(scanner.nextInt()));
        }
    }
}   

方法二:

/* a034. 二進位制轉換
*
* 2020/3/2
*/

import java.util.Scanner;

public class Pa034_2{

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

        while(scanner.hasNext()){
            int decNum = scanner.nextInt();

            boolean[] binNum = new boolean[(int)(Math.log(decNum) / Math.log(2)) + 1];

            for(int i = binNum.length - 1; i >= 0; i--){
                binNum[i] = decNum % 2 == 0? false: true; 
                decNum /= 2;
            } 

            for(int i = 0; i < binNum.length; i++){
                System.out.print(binNum[i]? 1: 0);
            }

            System.out.println();
        }
    }
}