题解 | #打印日期#

打印日期

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

#include <cstdio>

using namespace std;

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}
};

bool isLeapYear(int year) {			//判断是否为闰年
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
		return true;
	} 
	else {
		return false;
	}
}

int main() {
	int year, n;
	while ((scanf("%d%d", &year, &n)) != EOF) {
		int flag = isLeapYear(year);
		int month = 1;
		int day;
		while (n > dayTab[flag][month]) {
			n -= dayTab[flag][month];
			month++;
		}
		day = n;
		printf("%04d-%02d-%02d\n", year, month, day);
	}
	return 0;
}
全部评论

相关推荐

喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务