前往
大廳
主題

[OJ練習] 10082、10189

テリ君(桃夫模式) | 2022-11-25 16:09:35 | 巴幣 110 | 人氣 148

10082 (2/5)

#include <stdio.h>

char sen[1000];
char kb[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";

int main(){
    while(gets(sen)){
        for(int i = 0; sen[i]; i++){
            if(sen[i] == ' ') printf(" ");
            else{
                int j = 0;
                while(sen[i] != kb[j+1]) j++;
                printf("%c", kb[j]);
            }
        }
        printf("\n");
    }
    
    return 0;
}


10189 (2/5)

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


int main(){
    int w, h;
    int f_n = 0;
    char f[101][101];
    int count[101][101];
    
    while(scanf("%d %d", &h, &w)){
        if(h == 0 || w == 0 || h > 100 || w > 100) break;
        
        f_n++;
        
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                f[i][j] = '0';
                count[i][j] = 0;
            }
        }
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                f[i][j] = getchar();
                if(f[i][j] == '\n') f[i][j] = getchar();
            }
        }
        
        printf("Field #%d:\n", f_n);
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(f[i][j] == '*'){
                    if(i == 0 && j == 0){
                        count[i][j + 1]++;
                        count[i + 1][j]++;
                        count[i + 1][j + 1]++;
                    }
                    else if(i == 0 && j > 0 && j != w){
                        count[i][j + 1]++;
                        count[i][j - 1]++;
                        count[i + 1][j - 1]++;
                        count[i + 1][j]++;
                        count[i + 1][j + 1]++;
                    }
                    else if(i == 0 && j == w){
                        count[i][j - 1]++;
                        count[i + 1][j]++;
                        count[i + 1][j - 1]++;
                    }
                    else if(i > 0 && j == 0){
                        count[i][j + 1]++;
                        count[i - 1][j]++;
                        count[i - 1][j + 1]++;
                        count[i + 1][j]++;
                        count[i + 1][j + 1]++;
                    }
                    else if(i > 0 && j > 0 && j != w){
                        count[i][j - 1]++;
                        count[i][j + 1]++;
                        count[i - 1][j - 1]++;
                        count[i - 1][j]++;
                        count[i - 1][j + 1]++;
                        count[i + 1][j - 1]++;
                        count[i + 1][j]++;
                        count[i + 1][j + 1]++;
                    }
                    else if(i > 0 && j == w){
                        count[i][j - 1]++;
                        count[i - 1][j]++;
                        count[i - 1][j - 1]++;
                        count[i + 1][j]++;
                        count[i + 1][j - 1]++;
                    }
                    else if(i == h && j == 0){
                        count[i][j + 1]++;
                        count[i - 1][j]++;
                        count[i - 1][j + 1]++;
                    }
                    else if(i == h && j > 0 && j != w){
                        count[i][j - 1]++;
                        count[i][j + 1]++;
                        count[i - 1][j - 1]++;
                        count[i - 1][j]++;
                        count[i - 1][j + 1]++;
                    }
                    else{
                        count[i][j - 1]++;
                        count[i - 1][j]++;
                        count[i - 1][j - 1]++;
                    }
                }
            }
        }
        printf("\n");
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                printf("%c", f[i][j]);
            }
            printf("\n");
        }
        printf("\n");
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(f[i][j] == '*'){
                    printf("*");
                }
                else printf("%d", count[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    
    return 0;
}

10082需要知道的就是像這種題目就要先建立一個字元陣列去模擬題目的內容,如鍵盤。
10189我是用土法煉鋼,主要是我對陣列結構不熟悉所以就用比較穩的方式去做地雷數量判定。
然後非常感謝豆漿、啊雪、啊柴學長的協助不然這題真的做不出來……

題目要截圖很麻煩佔空間,想知道的就直接去OJ官網看相對應題號就好了。


創作回應

更多創作