用二维数组处理一年中每月的天数
#include <iostream>
using namespace std;
int days[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 y) {
bool flag = true;
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
flag = true;
} else {
flag = false;
}
return flag;
}
int main() {
int y, m, d;
bool isLeap;
while (cin >> y >> d) {
isLeap = isLeapYear(y);
m = 0;
while (days[isLeap][m] < d) {
d -= days[isLeap][m];
m++;
}
printf("%04d-%02d-%02d\n", y, m, d);
}
return 0;
}
用一维数组处理一年中每月的天数
#include <iostream>
using namespace std;
bool isLeapYear(int y) {
bool flag = true;
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
flag = true;
} else {
flag = false;
}
return flag;
}
int main() {
int y, m, d;
bool isLeap;
while (cin >> y >> d) {
int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
isLeap = isLeapYear(y);
if (isLeap) {
days[2]++; // 闰年2月多一天
}
m = 0;
while (days[m] < d) {
d -= days[m];
m++;
}
printf("%04d-%02d-%02d\n", y, m, d);
}
return 0;
}