Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
输入年和月,判断该月有几天?
Input
输入年和月,格式为年\月。
Output
输出该月的天数。
Example Input
2009\1
Example Output
31
Hint
注意判断闰年啊
Author
C语言实验——某年某月的天数(http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/1160.html)
题目代码:
#include <stdio.h>
#define MONTHS 12
int main()
{
int days[2][MONTHS] = { {31,29,31,30,31,30,31,31,30,31,30,31},
{ 31,28,31,30,31,30,31,31,30,31,30,31 } };
int year, month;
do {
scanf_s("%d\\%d", &year, &month);
} while (month < 1 || month>12);/*处理不合法数据的输入*/
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)/*闰年*/
{
printf("%d", days[0][month - 1]);
}
else/*非闰年*/
{
printf("%d", days[1][month - 1]);
}
return 0;
}
2018/6/1 这道题注意反义字符。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int y,m;
int month[12]={31,29,31,30,31,30,31,31,30,31,30,31};
scanf("%d\\%d",&y,&m);
if(m!=2)
printf("%d\n",month[m-1]);
else if(y%4==0&&y%100!=0||y%400==0){
printf("29\n");
}else{
printf("28\n");
}
return 0;
}