--- tags: DICE C --- 7-4 判斷閏年 === > [name=CHAWTeam] --- 目錄:[DICE C語言程式破解](https://hackmd.io/@CHAWTeam/DiceC) 上一篇:[7-3 五數比大小](https://hackmd.io/@CHAWTeam/DiceC-7-3) 下一篇:[8_1 奇數還是偶數?](https://hackmd.io/@CHAWTeam/DiceC-8_1) --- 題目 --- ```! 閏年是比普通年份多出一段時間的年份,在各種曆法中都有出現, 目的是為了彌補人為規定的紀年與地球公轉產生的差異。 目前使用的格里曆閏年規則如下: 1.西元年份除以4不可整除,為平年。 2.西元年份除以4可整除,且除以100不可整除,為閏年。 3.西元年份除以100可整除,且除以400不可整除,為平年 4.西元年份除以400可整除,為閏年。 (優先順序:4>3>2) 寫一個程式,根據輸入的西元年分(正整數),判斷後輸出「平年」或「閏年」。 因為此題輸出有多種狀況,故需將程式碼複製4次,都判斷正確才給分,每個輸出都要換行。 輸入範例1: 2019 輸出範例1: 平年 輸入範例2: 2020 輸出範例2: 閏年 輸入範例3: 2100 輸出範例3: 平年 ``` 程式碼 --- ### 範例1 ```c= #include <stdio.h> int main() { int year, t = 0; // t 預設為平年 scanf("%d", &year); if (year % 4 == 0) if (year % 100 != 0) //符合閏年規則 2 即為閏年 t = 1; if (year % 100 == 0) if (year % 400 == 0) //符合閏年規則 4 即為閏年 t = 1; if (t == 0) printf("平年\n"); else printf("閏年\n"); //第二輪判斷 scanf("%d", &year); if (year % 4 == 0) if (year % 100 != 0) t = 1; if (year % 100 == 0) if (year % 400 == 0) t = 1; if (t == 0) printf("平年\n"); else printf("閏年\n"); //第三輪測資 scanf("%d", &year); if (year % 4 == 0) if (year % 100 != 0) t = 1; if (year % 100 == 0) if (year % 400 == 0) t = 1; if (t == 0) printf("平年\n"); else printf("閏年\n"); //第四輪測資 scanf("%d", &year); if (year % 4 == 0) if (year % 100 != 0) t = 1; if (year % 100 == 0) if (year % 400 == 0) t = 1; if (t == 0) printf("平年\n"); else printf("閏年\n"); return 0; } ``` ### 範例2 ```c= #include <stdio.h> int main() { int year, t = 0; scanf("%d", &year); if (year % 4 == 0 && year % 100 != 0) t = 1; if (year % 100 == 0 && year % 400 == 0) t = 1; if (t == 0) printf("平年\n"); else printf("閏年\n"); //第二輪測資 scanf("%d", &year); if (year % 4 == 0 && year % 100 != 0) t = 1; if (year % 100 == 0 && year % 400 == 0) t = 1; if (t == 0) printf("平年\n"); else printf("閏年\n"); //第三輪測資 scanf("%d", &year); if (year % 4 == 0 && year % 100 != 0) t = 1; if (year % 100 == 0 && year % 400 == 0) t = 1; if (t == 0) printf("平年\n"); else printf("閏年\n"); //第四輪測資 scanf("%d", &year); if (year % 4 == 0 && year % 100 != 0) t = 1; if (year % 100 == 0 && year % 400 == 0) t = 1; if (t == 0) printf("平年\n"); else printf("閏年\n"); return 0; } ``` 輸出 --- ```! 2019 平年 2020 閏年 2100 平年 2020 閏年 ``` --- [查看我們在HackMD上的所有筆記](https://hackmd.io/@CHAWTeam) 目錄:[DICE C語言程式破解](https://hackmd.io/@CHAWTeam/DiceC) 上一篇:[7-3 五數比大小](https://hackmd.io/@CHAWTeam/DiceC-7-3) 下一篇:[8_1 奇數還是偶數?](https://hackmd.io/@CHAWTeam/DiceC-8_1) --- {%hackmd Iiu5mOixR7yWkPHKCkabBg %} <iframe class="LikeCoin" height="235" src="https://button.like.co/in/embed/chawteam/button?referrer=https://hackmd.io/@CHAWTeam/DiceC-7-4" width="100%"></iframe> --- {%hackmd i1nMRrZcTFmTvoF897K9zg %}
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up