文章

2

粉丝

310

获赞

0

访问

26.4k

头像
50%通过率,请大佬帮忙看下问题。
P1051 中南大学机试题
发布于2020年4月6日 12:21
阅读数 8.5k

#include<iostream>

using namespace std;

bool isryear(int n) {
    if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) {
        return true;
    }
    else {
        return false;
    }
}

int main() {
    int y, m, d;
    while (cin >> y >> m >> d) {
        if ((m < 1 || m>12) || (d > 31 || d < 1)) {
            cout << "Input error!";
            continue;
        }
        else if ((d > 31 || d < 1)) {
            cout << "Input error!";
        }
      ...

登录查看完整内容


登录后发布评论

2 条评论
chtfc
2020年4月6日 19:26

重新写下年月日的判定吧

2001 4 31 这组样例在你程序里就不行

 

赞(2)

听张志毅的话 : 回复 chtfc: 谢谢 已经AC #include<iostream> using namespace std; bool isryear(int n) { if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) { return true; } else { return false; } } int main() { int y, m, d; int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; int b[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 }; while (cin >> y >> m >> d) { if ((m < 1 || m>12)) { cout << "Input error!"<<endl; continue; } else if (isryear(y)&&(d >b[m]|| d < 1)) { cout << "Input error!"<<endl; } else if (!isryear(y) && (d > a[m] || d < 1)) { cout << "Input error!"<<endl; } else { int sum = 0; int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; int b[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 }; if (isryear(y)) { for (int i = 0; i < m; i++) { sum += b[i]; } } else { for (int i = 0; i < m; i++) { sum += a[i]; } } cout << sum + d << endl; } } return 0; }

2020年4月6日 20:42