#include<stdio.h> int main() { int n,y,m,d,w,key; int a[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} }; scanf("%d",&n); while(n--) { scanf("%d%d%d%d",&y,&m,&d,&w); key=0; if(y%4==0&&y%100!=0||y%400==0) key=1; d=d+w;//直接把日加上能加的天数 while(d>a[key][m])//不是这个月 { d=d-a[key][m];//减去天数 m++;//月份加1 if(m>12){//年份加一 m=1; y++; key=0;//重新判断闰年 if(y%4==0&&y%100!=0||y%400==0) key=1; } } printf("%d-%02d-%02d\n",y,m,d); } }
#include <cstdio> using namespace std; struct Date{ int y; int m; int d; }; int no_month_days[12]{31,28,31,30,31,30,31,31,30,31,30,31}; int leap_month_days[12]{31,29,31,30,31,30,31,31,30,31,30,31}; bool IsLeapYear(int y){ return y%4==0&&y%100!=0||y%400==0; } Date operator+(Date date,int add_days){ date.d+=add_days; while(1){ int *month_days; month_days=IsLeapYear(date.y)?leap_month_days:no_month_days; if(date.d<=month_days[date.m-1]) break; else{ date.d-=month_days[date.m-1];date.m++; if(date.m>12){ date.y++;date.m=1; } } } return date; } int main(){ int n,y,m,d,add_days; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d%d%d%d",&y,&m,&d,&add_days); Date res=Date{y,m,d}+add_days; printf("%04d-%02d-%02d\n",res.y,res.m,res.d); } return 0; }
import java.text.DecimalFormat; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { int year = scanner.nextInt(); int month = scanner.nextInt(); int day = scanner.nextInt(); int num = scanner.nextInt(); LocalDate date = LocalDate.of(year, month, day); LocalDate date1 = date.plus(num, ChronoUnit.DAYS); DecimalFormat f = new DecimalFormat("00"); System.out.println(date1.getYear()+"-"+f.format(date1.getMonth().getValue())+"-"+f.format(date1.getDayOfMonth())); } } }
#include<iostream> using namespace std; int month[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}}; int isleap(int y) { if((y%4==0&&y%100!=0)||(y%400==0)) return 1; //是闰年 else return 0; } int main() { int m; cin>>m; for(int i=0;i<m;i++) { int y,m,d,n; cin>>y>>m>>d>>n; for(int j=0;j<n;j++) { int flag=isleap(y); d++; if(d>month[m][flag]) { d=1; m++; if(m>12) { m=1; y++; } } } printf("%04d-%02d-%02d\n",y,m,d); } return 0; }很好理解的一种
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <vector> #include <map> #include <cmath> #include <set> #include <queue> using namespace std; bool pd(int y) { if(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) return true; return false; } int months[] ={0,31,28,31,30,31,30,31,31,30,31,30,31}; int main() { int n; int nowYear,nowMonth,nowDay; int add; cin >> n; for(int i = 0 ; i<n;i++) { cin >> nowYear >> nowMonth >> nowDay >> add; while(add) { if(pd(nowYear)) months[2] = 29; else months[2] = 28; if(add >= months[nowMonth] - nowDay + 1) { add = add - (months[nowMonth] - nowDay + 1); nowDay = 1; nowMonth++; if(nowMonth > 12) { nowMonth = 1; nowYear++; } } else { nowDay += add; add = 0; } } printf("%04d-%02d-%02d\n",nowYear,nowMonth,nowDay); } }
#include <stdio.h> #define ISYEAP(x) x%4==0&&x%100!=0||x%400==0?1:0 int main() { int n,y,m,d,x; int a[13][2]= { 0,0, 31,31, 28,29, 31,31, 30,30, 31,31, 30,30, 31,31, 31,31, 30,30, 31,31, 30,30, 31,31 }; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d%d%d",&y,&m,&d,&x); for(int i=0;i<x;i++) { d++; if(d>a[m][ISYEAP(y)]) { d=1; m++; if(m>12) { m=1; y++; } } } printf("%04d-%02d-%02d\n",y,m,d); } return 0; }代码不短,但是很好理解的
#include<iostream> #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){ return (year%4==0 && year%100!=0)||(year%400==0); } //平年闰年天数 int NumberOfYear(int year){ if(IsLeapYear(year)){ return 366; } else{ return 365; } } //主函数 int main(){ int year,month,day; int number; int caseNumber; cin>>caseNumber; while(caseNumber--){ cin>>year>>month>>day>>number; for(int i=0; i<month; i++){ number += daytab[IsLeapYear(year)][i]; } number += day;//至此将加上若干天后的日期转换为数字,接下来再换回日期reverse! month = 0; while(number>NumberOfYear(year)){ number -= NumberOfYear(year); year += 1; }//对年份的处理 while(number>daytab[IsLeapYear(year)][month]){ number -= daytab[IsLeapYear(year)][month]; month += 1; }//对月份的处理 day = number;//最后对日期的处理 printf("%04d-%02d-%02d\n",year,month,day); } return 0; }
#include<iostream> #include<cstring> using namespace std; bool leapyear(int y) { if (y % 100 == 0 && y % 400 == 0 || y % 100 != 0 && y % 4 == 0) return true; else return false; } int fun(int y, int m, int d, int n) { int sum = 0; int mon = 0, date = 0; int month[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; if (leapyear(y)) { month[2] = 29; } for (int i = 0; i < m; i++) { sum += month[i]; } sum = sum + d + n; while (sum >= 365) { if (leapyear(y) && (sum / 366 != 0)) { y++; sum -= 366; } if ((!leapyear(y)) && (sum / 365 != 0)) { y++; sum -= 365; } } if (leapyear(y)) { month[2] = 29; } if (!leapyear(y)) { month[2] = 28; } if (sum == 0) { y--; mon = 12; date = 31; printf("%04d-%02d-%02d\n", y, mon, date); } if (sum != 0) { int temp = 0; for (int i = 1; i <= 12; i++) { temp += month[i]; mon++; if (temp >= sum) { temp -= month[i]; date = sum - temp; break; } } printf("%04d-%02d-%02d\n", y, mon, date); } return 0; } int main() { int m, year, mon, date, n; while (cin >> m) { for (int i = 0; i < m; i++) { cin >> year >> mon >> date >> n; fun(year, mon, date, n); } } return 0; }
import datetime m = input() time_set = [] for i in range(int(m)): temp = input().split() time_set.append(temp) for temp_time in time_set: time1 = datetime.date(int(temp_time[0]), int(temp_time[1]), int(temp_time[2])) time2 = time1 + datetime.timedelta(days = int(temp_time[3])) print(time2)
#include <iostream> using namespace std; int Get_monthday(int year,int month) { int arr_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if(((year%4==0&&year%100!=0)||(year%400==0))&&month==2) { return 29; } return arr_month[month]; } int main() { int m=0; cin>>m; int year=0; int month=1; int day=1; int add_day=0; while(m-->0) { cin>>year>>month>>day>>add_day; day+=add_day; while(day>Get_monthday(year, month)) { day-=Get_monthday(year, month); month++; if(month==13) { month=1; year++; } } printf("%04d-%02d-%02d\n",year,month,day); } return 0; } // 64 位输出请用 printf("%lld")
#include <climits> #include <iostream> using namespace std; class Date { public: //重写构造函数,用于cin输入的year、month、day对Date对象初始化 Date(int year=0,int month =0,int day=0) :_year(year), _month(month), _day(day) { } //打印输出,用于题目符合格式的输出 void Print() { printf("%04d-%02d-%02d\n",_year,_month,_day); //cout<<_year<<"-"<<_month<<"-"<<_day<<endl; } //获取当前年月,具体有多少天,方便日期+后进位 int GetMonthDays(int year,int month) { int monthDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int retDays = monthDays[month]; if((year%4==0&&year%100!=0||year%400==0)&&month==2) { ++retDays; } return retDays; } //运算符重载,用于Date对象进行日期+= Date& operator+=(int x) { _day+=x; while(_day>GetMonthDays(_year,_month)) { _day-=GetMonthDays(_year,_month); ++_month; if(_month>12) { _month = 1; ++_year; } } return *this; } private: int _year; int _month; int _day; }; int main() { int m; cin>>m; int icount = m; Date d[m]; //按题目意思,先进行m组的输入,每输入一组,对一个Date对象进行初始化并进行+= while(icount) { int year=0,month=0,day=0,adddays=0; cin>>year>>month>>day>>adddays; d[icount] = Date(year,month,day); d[icount]+=adddays; icount--; } //将m组Date对象进行打印输出 for(int i=m;i>0;--i) { d[i].Print(); } }
#include <iostream> using namespace std; int GetMonthDay(int year, int month) { int Month_day[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))) { return 29; } return Month_day[month]; } int main() { int _year, _month, _day; int day; int n = 0; cin >> n; while (n--) { // 注意 while 处理多个 case cin >> _year >> _month >> _day >> day; _day += day; int month_day = GetMonthDay(_year, _month); while (_day > month_day) { ++_month; if (_month == 13) { ++_year; _month = 1; } _day -= month_day; month_day = GetMonthDay(_year, _month); } printf("%04d-%02d-%02d\n", _year, _month, _day); } }
#include <iostream> using namespace std; class Date { public: Date(int year = 0, int month = 0, int day = 1) { _year = year; _month = month; _day = day; } Date& calucateDays(int days) { int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; _day += days; while (_day > monthDays[this->_month]) { if (this->_month == 2 && (((_year % 4 == 0) && (_year % 100 != 0)) || _year % 400 == 0)) { _day -= 29; } else { _day -= monthDays[this->_month]; } ++this->_month; if (this->_month == 13) { this->_month = 1; this->_year++; } } return *this; } void Print() { printf("%d-%02d-%02d\n", _year, _month, _day); } private: int _year; int _month; int _day; }; int main() { int n = 0; cin >> n; int year, month, day, days; Date* dateArray = new Date[n]; for (int i = 0; i < n; ++i) { cin >> year >> month >> day >> days; dateArray[i] = Date(year, month, day); // 拷贝构造 dateArray[i].calucateDays(days); // 每个对象调用函数 } for (int i = 0; i < n; ++i) { dateArray[i].Print(); } return 0; }