PTA 7-458 某月几天
分数 10
作者 黄龙军
单位 绍兴文理学院
输入月份month和年份year,判断该月的天数。其中,month是一个3个字母表示的月份,1-12月份分别表示为:Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec。另外,若某年份是闰年,则该年份能被4整除但不能被100整除或者能被400整除。
输入格式:
测试数据有多组,处理到文件尾。对于每组测试,输入一个仅包含3个字母的字符串和一个整数,分别表示月份month和年份year。
输出格式:
对于每组测试,输出该年该月的天数。
输入样例:
Feb 2020
Dec 2022
输出样例:
29
31
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <stdio.h>
int main(){
char month[][10] = {
" ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
}; //月份
int day[10] = {28, 30, 31}; //月份天数
char mnt[10]; //输入月份
int year; //输入年份
while(~scanf("%s %d", &mnt, &year)){
day[0] = 28;
if(year%4==0 && year%100!=0 || year%400==0){
day[0] = 29;
}
if(strcmp(mnt, month[2]) == 0){ //2月
printf("%d\n", day[0]);
}else if( //4,6,9,11月
strcmp(mnt, month[4]) == 0 || strcmp(mnt, month[6]) == 0
|| strcmp(mnt, month[9]) == 0 || strcmp(mnt, month[11]) == 0
){
printf("%d\n", day[1]);
}else{ //1,3,5,7,8,10,12月
printf("%d\n", day[2]);
}
}
return 0;
}
解题思路:
注意判断输入的年份是否闰年
归属知识点:
数组
选择结构
字符串函数
博客围绕PTA 7-458题目,要求输入月份和年份判断该月天数。月份用3个字母表示,需判断年份是否为闰年。给出输入输出格式及样例,解题要注意判断闰年,涉及数组、选择结构和字符串函数等知识点,使用C语言解答。
1573

被折叠的 条评论
为什么被折叠?



