文章
28
粉丝
230
获赞
23
访问
242.6k
#include <iostream>
#include <cstdio>
using namespace std;
int Data[2][13] = {
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
bool isLeapYear(int year){//判断是否是闰年
return (year % 100 != 0 && year % 4 == 0) || (year % 400 == 0);//判定闰年的经典语句
}
int main(int argc, char const *argv[])//今天是第几天
{
int day,month,year;
while (scanf("%d%d%d",&year,&month,&day)!= EOF){//注:这个操作均在while循环内进行,否则key作用域出错
int number = 0;//累计日期
int key = isLeapYear(year);
for (int i = 0; i < month; ++i)
{
number = number + Data [key][i];
}
if(day > Data[key][month]||year<0 || month <1 || month >12 ){//最容易忽视的错误是:输入的日期与月份不匹配,解决方案:与Data中的该月最大天数做对比
printf("Input error!\n");
}else{
number=number + day ;
printf("%d\n",number);
}
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发