輸入年月日時分秒,輸出該年月日時分秒的下一秒

2021-07-29 03:52:35 字數 1174 閱讀 5024

編寫乙個函式,要求輸入年月日時分秒,輸出該年月日時分秒的下一秒。如輸入2023年12月31日23時59分59秒,則輸出2023年1月1日0時0分0秒

考慮如何幾種情形

演算法思路:

(1)判斷現在月份是大月還是小月

大月:1月,3月,5月,7月,8月,10月,12月(31天)

小月:2月(28天或者29天),4月,6月,9月,11月(30天)

(2)2月份要判斷是平年還是閏年,平年28天,閏年29天

(3)考慮幾種邊界情況:

(3.1) year<0||month<1||month>12||date<1||date>31||hour<0||hour>23||minute<0||minute>59||second<0||second>60

考慮定義乙個陣列

month_days[12]=

閏年時month_days[1]=29,閏年判斷條件如下:

year%400==0||year%100!=0&&year%4==0

second+=1;

if(second==60)

else

break;

default:

days=30;

break;

}if(date>days)

if(month>12)}}

}}

完整**:

#include#includeusing namespace std;

int nexttime(int year,int month,int

date,int hour,int minute,int second)

else

break;

default:

days=30;

break;

}if(date>days)

if(month>12)}}

}cout<

<

<

int main()

測試用例:

獲取年月日時分秒

calendar ca calendar.getinstance int year ca.get calendar.year 獲取年份 2016 system.out.println year int month ca.get calendar.month 獲取月份 10 1 system.out....

python年月日時分秒

通過datetime函式獲取 import datetime 取當前時間 print datetime.datetime.now 取年 print datetime.datetime.now year 取月 print datetime.datetime.now month 取日 print dat...

輸入年月日時分秒,計算下一秒的年月日時分秒

1.名稱 時光飛逝 2.描述 輸入年月日時分秒,計算下一秒的年月日時分秒 3.分析 1.定義六個全域性變數 2.輸入時間 3.計算下一秒 3.1 second加一秒 3.2 計算每月對應的天數 3.3 計算閏年 4.列印 4.模組 1.輸入模組 void input 2.計算下一秒 void nex...