题解 | #打印日期#

打印日期

http://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int year, day, month, _day;
    int* m = new int[12]{ 31,28,31,30,31,30,31,31,30,31,30,31 };
    while (cin >> year >> day) {
        bool r = false;
        month = 0;
        if ((year % 4 == 0 && year % 100) || year % 400 == 0)
            r = true;
        for (int i = 1; i <= 12; ++i) {
            if ((i != 2 && day > m[i - 1]) || (r && i == 2 && day > 29) || (i == 2 && !r && day > m[i - 1])) {
                month = i;
                day -= ((r == true && i == 2 )? (m[i - 1] + 1): m[i - 1]);
                _day = day;
            }
            else {
                if (day != 0) {
                    month += 1;
                    _day = day;
                }
                break;
            }
        }
        string yearstr = to_string(year);
        cout << yearstr << "-" << ((to_string(month).size() == 1) ? ("0" + to_string(month)) : (to_string(month))) << "-" << ((to_string(_day).size() == 1) ? ("0" + to_string(_day)) : (to_string(_day))) << endl;
    }
}
全部评论

相关推荐

牛客771574427号:恭喜你,华杰
点赞 评论 收藏
分享
10-06 12:46
门头沟学院 Java
跨考小白:定时任务启动
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务