题解 | #打印日期#
打印日期
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;
}
}