tags: C636 C

C636. 十二生肖


題目描述

內容

民國元年

(1912) 是鼠年。
給定若干個民國年份 -
100
~
107
,請輸出該年生肖為何。

輸入說明

每行各有一個整數

n,表示民國年分。

輸出說明

每行各輸出一個中文字,表示對應的生肖。

評分說明

第一題組

(100 %
)
:-
100n107


範例

測資 1:

​​​​1
​​​​107

輸出 1:

​​​​鼠
​​​​狗

測資 2:

​​​​-1
​​​​10
​​​​-100

輸出 2:

​​​​豬
​​​​雞
​​​​猴

題目思索

因為沒給要輸入之行數,所以改成EOF形式判斷輸入是否結束。

while(scanf("%d", &n) != EOF){
    //底下的程式
}

再來只要將輸入進來的

n 進行判斷並輸出即可。

    n = (n + 120) % 12;
    switch(n){
        case 1:
            printf("鼠\n"); break;
        case 2:
            printf("牛\n"); break;
        case 3:
            printf("虎\n"); break;
        case 4:
            printf("兔\n"); break;
        case 5:
            printf("龍\n"); break;
        case 6:
            printf("蛇\n"); break;
        case 7:
            printf("馬\n"); break;
        case 8:
            printf("羊\n"); break;
        case 9:
            printf("猴\n"); break;
        case 10:
            printf("雞\n"); break;
        case 11:
            printf("狗\n"); break;
        default:        //當 n 為零時
            printf("豬\n"); break;
  	}

初稿程式碼

#include <stdio.h> #include <string.h> int main(){ int n; while(scanf("%d", &n) != EOF){ n = (n + 120) % 12; switch(n){ case 1: printf("鼠\n"); break; case 2: printf("牛\n"); break; case 3: printf("虎\n"); break; case 4: printf("兔\n"); break; case 5: printf("龍\n"); break; case 6: printf("蛇\n"); break; case 7: printf("馬\n"); break; case 8: printf("羊\n"); break; case 9: printf("猴\n"); break; case 10: printf("雞\n"); break; case 11: printf("狗\n"); break; default: printf("豬\n"); break; } } return 0; }

測試結果

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


嘗試更正

思考了一下,因為沒有民國

0,所以之前的程式
n
在負數時無法輸出對應的生肖,於是在
n
為負數時再加上
1
以補齊,順便整理了版面。

if(n < 0)
    n++;

更正後測試結果

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


成果

程式碼

#include <stdio.h> #include <string.h> int main(){ int n, c; while(scanf("%d", &n) != EOF){ if(n < 0) n++; c = (n + 120) % 12; switch(c){ case 1: printf("鼠\n"); break; case 2: printf("牛\n"); break; case 3: printf("虎\n"); break; case 4: printf("兔\n"); break; case 5: printf("龍\n"); break; case 6: printf("蛇\n"); break; case 7: printf("馬\n"); break; case 8: printf("羊\n"); break; case 9: printf("猴\n"); break; case 10: printf("雞\n"); break; case 11: printf("狗\n"); break; default: printf("豬\n"); break; } } return 0; }

解題

c636res


反思

這次想得不夠完善,以至於出錯,好在之後有發現問題所在,也就是民國

0 年並不存在,最後得以理出正確的思緒。


題目來源

c636. 十二生肖 (From Zero Judge)

2024-4-7 完成編寫此文