题解 | #打印日期#

打印日期

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

#include <iostream>
using namespace std;
#include <iomanip>
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
	int y,n,flag = 0;
	while(cin >>y>>n){
		int i,temp = n;
		//判断闰年
		if (y % 4 == 0 && y %100 != 0 || y%400 == 0){
            month[2]=29;
        }
		for(i = 1; temp>0; i++){//i 月份
			if (temp>month[i])
				temp-=month[i];
			else break;
		}
		
		cout << y <<"-"<< setw(2) <<setfill('0')<<i<<"-"<< setw(2) <<setfill('0')<<temp<<endl;
        month[2] = 28;//还原2月份天数
	}
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务