ETH官方钱包

前往
大廳
主題

[OJ練習] 11764、401 其實解完後就是三個字,爽快

テリ君(桃夫模式) | 2022-12-26 13:12:06 | 巴幣 1004 | 人氣 211


11764(1/5)

#include <stdio.h>
#include <stdlib.h>

int main(){
    int T; // test cases
    scanf("%d", &T);
    
    int *N, **num;
    int *hj, *lj; // low jumps, high jumps
    
    N = (int *)malloc(sizeof(int) * T);
    hj = (int *)malloc(sizeof(int) * T);
    lj = (int *)malloc(sizeof(int) * T);
    num = (int **)malloc(sizeof(int) * T); // int 2D
    
    for(int i = 0; i < T; i++){
        scanf("%d", N + i);
        *(num + i) = (int*)malloc(sizeof(int) * *(N + i)); // int 1D
        for(int j = 0; j < *(N + i); j++){
            scanf("%d", (*(num + i) + j));
        }
    }
    
    for(int i = 0; i < T; i++){
        
        *(hj + i) = 0;
        *(lj + i) = 0;
        
        for(int j = 0; j < *(N + i); j++){
            if(j != 0){
                if(*(*(num + i) + j - 1) < *(*(num + i) + j)){
                    int temp_h = 0;
                    temp_h = *(hj + i) + 1;
                    *(hj + i) = temp_h;
                }
                else if(*(*(num + i) + j - 1) > *(*(num + i) + j)){
                    int temp_l = 0;
                    temp_l = *(lj + i) + 1;
                    *(lj + i) = temp_l;
                }
            }
        }
    }
    
    for(int i = 0; i < T; i++){
        printf("Case %d: %d %d\n", i + 1, *(hj + i), *(lj + i));
    }
    
    for(int i = 0; i < T; i++){
        free(*(num + i));
    }
    
    free(num);
    free(N);
    free(hj);
    free(lj);
    

    return 0;
}

401(2/5)

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

static char rev(char c){
    switch(c){
        case 'A':
            return 'A';
        case 'E':
            return '3';
        case 'H':
            return 'H';
        case 'I':
            return 'I';
        case 'J':
            return 'L';
        case 'L':
            return 'J';
        case 'M':
            return 'M';
        case 'O':
            return 'O';
        case 'S':
            return '2';
        case 'T':
            return 'T';
        case 'U':
            return 'U';
        case 'V':
            return 'V';
        case 'W':
            return 'W';
        case 'X':
            return 'X';
        case 'Y':
            return 'Y';
        case 'Z':
            return '5';
        case '1':
            return '1';
        case '2':
            return 'S';
        case '3':
            return 'E';
        case '5':
            return 'Z';
        case '8':
            return '8';
            
        default:
            return c;
        
    }
}

int result(int len, char line, char temp){
    
    int check;
    
    if(line != temp){
        if(line != rev(temp)){
            check = 1;
        }
        else{
            check = 2;
        }
    }
    else{
        if(line != rev(temp)){
            check = 3;
        }
        else{
            check = 4;
        }
    }
    
    return check;
}

int main(){
    
    char line[256];
    while(scanf("%s", line)){
        int len = 0;
        for(int i = 0; line[i] != '\0'; i++){
            len++;
        }
        
        bool c1 = false, c2 = false , c3 = false , c4 = false;
        
        if(len % 2 == 0){ // for even len
            int temp = len - 1;
            for(int i = 0; i < len / 2; i++){
                if(result(len, line[i], line[temp]) == 1){
                    c1 = true;
                }
                else if(result(len, line[i], line[temp]) == 2){
                    c2 = true;
                }
                else if(result(len, line[i], line[temp]) == 3){
                    c3 = true;
                }
                else{
                    c4 = true;
                }
                temp--;
            }
        }
        else{ // for odd len
            int temp = len - 1;
            for(int i = 0; i < (len - 1) / 2; i++){
                if(result(len, line[i], line[temp]) == 1){
                    c1 = true;
                }
                else if(result(len, line[i], line[temp]) == 2){
                    c2 = true;
                }
                else if(result(len, line[i], line[temp]) == 3){
                    c3 = true;
                }
                else{
                    c4 = true;
                }
                temp--;
            }
        }
        if(c1){
            for(int i = 0; i < len; i++){
                printf("%c", line[i]);
            }
            printf(" -- is not a palindrome.\n");
        }
        else if(c2){
            for(int i = 0; i < len; i++){
                printf("%c", line[i]);
            }
            printf(" -- is a mirrored string.\n");
        }
        else if(c3){
            for(int i = 0; i < len; i++){
                printf("%c", line[i]);
            }
            printf(" -- is a regular palindrome.\n");
        }
        else{
            for(int i = 0; i < len; i++){
                printf("%c", line[i]);
            }
            printf(" -- is a mirrored palindrome.\n");
        }
        
        
        fflush(stdin);
    }
    
    return 0;
}

解完這兩題我只能說 : 爽死。

11764其實直接宣告二維陣列也不是不行,只是我想要打鐵趁熱多練雙指標,
所以就做了,當中在宣告指標的時候一度亂掉,還好後來慢慢整理起來就又沒事了。
就,其實熟能生巧而已。

401這題就好玩了,我第一個想到的是之前解過的一題LeetCode → Roman To Integar :
這題需要用到Static 去做 case的比對,反正題目有給整個表格,所以一開始優先就是先把Static搞好。
然後我的思維是做逐步check,因為這個判定是分支樹狀的,其實有點類似DFS,所以我的結構就是一個類DFS,最終結果出來就是如此,加個function讓迴圈去跑function就可以了。

這題我整理和思考了好一陣子,今天這兩題真的是做的過程很不舒服,但做完真的好爽喔= =
而且就像學長說的,做的越痛苦,成功後就是成長。
阿硬要的話其實401也是可以用指標,但就,先這樣ㄅXD

好了,吃午餐去。

創作回應

更多創作