日期类

题目描述
编写一个日期类,要求按xxxx-xx-xx 的格式输出日期,实现加一天的操作。

输入描述:
输入第一行表示测试用例的个数m,接下来m行每行有3个用空格隔开的整数,分别表示年月日。测试数据不会有闰年。

输出描述:
输出m行。按xxxx-xx-xx的格式输出,表示输入日期的后一天的日期。

示例1
输入
2
1999 10 20
2001 1 31
输出
1999-10-21
2001-02-01
备注:
注意个位数日期前面要有0

代码:

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;

int main()
{
	int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
	int n , year , month , day;
	while(cin >> n){
		for(int i = 0; i < n; i++){
			cin >> year >> month >> day;
			if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
				months[1] = 29;
			}else{
				months[1] = 28;
			}
			if((day + 1) > months[month - 1]){
				month++;
				if(month > 12){
					year++;
				}
				month = month % 12;
				day = 1;
			}else{
				day++;
			}
			cout << year << "-";
			if(month < 10){
				cout << "0" << month << "-";
			}else{
				cout << month << "-";
			}
			if(day < 10){
				cout << "0" << day << endl;;
			}else{
				cout << day << endl;
			}
		}
	} 
    return 0;
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值