文章

34

粉丝

67

获赞

7

访问

7.9k

头像
日期计算 题解:
P1051 中南大学机试题
发布于2024年2月23日 19:41
阅读数 290

定义一个二维数组 存储 闰年和非闰年的每个月都有多少天 .

#include <bits/stdc++.h>

 

using namespace std;

 

struct data {

  int year, month, day;

} p;

 

bool Isrunnian(int year) {

  if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

    return true;

  } else {

    return false;

  }

}

 

int dayTab[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}};

 

int main() {

 

  while (scanf("%d %d %d", &p.year, &p.month, &p.day) != EOF) {

    if (p.month >= 0 && p.day >= 0 && p.year >= 0 && p.month <= 12 &&

        p.day <= dayTab[Isrunnian(p.year)][p.month]) {  // 判断输入是否符合规定,年份可以任意,但是要大于等于0, 天数就可以使用数组中的每月天数来判断是...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发