当前位置:网站首页>Daily exercise: a series of dates
Daily exercise: a series of dates
2022-07-05 17:48:00 【Sharp blade CC】
Calculate date to day conversion
link : Calculate date to day conversion
Ideas 1: The conventional idea is to use while Cycle from the first day of this month to that day , But there are still very simple ideas , That is, the following idea 2 , There is also mainly about train of thought 2 .
Train of thought two : Use an array to store the accumulated days of each month , If the first month is 31 God , The second month is storage 31+28=59 God , And so on , It's good to store like this .
We ask that the total number of days from this year to a certain day is the month before this month , And the sum of all the days before this month plus the days of this month ! But don't forget to judge whether this year is a leap year and whether this day exceeds February ( Because there is no need to add another day before February )
Code :
#include<iostream>
using namespace std;
int main()
{
int arr[13] = {
0,31,59,90,120,151,181,212,243,273,304,334,365};
int year, month, day;
cin >> year >> month >> day;
// Give Way n Assign the total number of days in the previous month from this year to this month plus the current number of days day
int n = arr[month - 1] + day;
// Remember to judge whether it is a leap year
if(month > 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
n++;
cout << n;
return 0;
}
Date accumulation
link : Date accumulation
Ideas : Because this problem requires m Test cases , So cycle m All over . And use nowday Record the maximum number of days in the month , For later judgment .
And then use while Sub loop , take addday The days accumulated to day and month On , then addday–.
Each cycle judges whether the current day exceeds the maximum number of days in the current month , And whether the month exceeds 12 month .
Code :
#include<iostream>
using namespace std;
int main()
{
int arr[13] = {
0,31,28,31,30,31,30,31,31,30,31,30,31};
int m = 0;
cin >> m;
int year = 0, month = 1, day = 1, addday = 0;
// Because there are multiple sets of test cases , So use m To cycle
for(int i=0; i<m; i++)
{
cin >> year >> month >> day >> addday;
int nowday = arr[month];// use nowday Record the maximum number of days in the month
while(addday > 0)
{
day++;
if((month == 2) && ((year%4==0 && year%100!=0) || (year%400==0)))
{
nowday=29;
}
// Judge whether the number of days is greater than the maximum number of days in the month
if(day > nowday)
{
day = 1;
month++;
// Determine whether the number of months is greater than 12
if(month > 12)
{
month=1;
year++;
}
nowday=arr[month];
}
addday--;
}
// Print the days and months in format
if(day<10 && month<10)
cout<<year<<"-0"<<month<<"-0"<<day<<endl;
else if(day<10 && month>=10)
cout<<year<<'-'<<month<<"-0"<<day<<endl;
else if(day>=10 && month<10)
cout<<year<<"-0"<<month<<'-'<<day<<endl;
else
cout<<year<<'-'<<month<<'-'<<day<<endl;
}
return 0;
}
Print date
link : Print date
Ideas : First use sday Total storage days , And then sday Judge whether this year is a leap year , And then use while The cycle will sday Split into day and month, Every time day++,sday–, until sday be equal to 0.
Then remember to judge day Is the maximum number of days of the month reached , If yes, reset , We also need to judge whether the month is greater than 12, If yes, reset it .
Code :
#include<iostream>
using namespace std;
int main()
{
int arr[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31};
int year=0,month=1,day=0,sday=0;
cin>>year>>sday;
// Judge whether it is a leap year
if((year % 4 ==0 && year % 100 != 0) || (year % 400 == 0))
arr[2]=29;
// Convert the total number of days into months and days
while(sday > 0)
{
day++;
if(day > arr[month])
{
day=1;
month++;
if(month > 12)
{
month=1;
year++;
}
}
sday--;
}
// Determine the format of printing days and months
if(day<10 && month<10)
cout<<year<<"-0"<<month<<"-0"<<day<<endl;
else if(day<10 && month>=10)
cout<<year<<'-'<<month<<"-0"<<day<<endl;
else if(day>=10 && month<10)
cout<<year<<"-0"<<month<<'-'<<day<<endl;
else
cout<<year<<'-'<<month<<'-'<<day<<endl;
// Because there are multiple sets of test cases , All to reset
arr[2]=28;
month=day=1;
return 0;
}
Date difference
link : Date difference
Ideas : First save the year, month and day of the two numbers with three variables , keep max Some are big dates ,min For small dates .
And then start the cycle , until min The date of is equal to max Until mm / DD / yyyy , use count To count the number of days between them .
Remember to judge whether the sky exceeds Whether the number of days and months of the month exceeds 12 month , Reset if you have any !
Code :
#include<iostream>
using namespace std;
int main()
{
int arr[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31};
int day1,day2,count=0;
cin>>day1>>day2;
// keep max For the big number , Then decompose it
int max=day1>day2?day1:day2;
int maxday=max%100;
int maxmonth=(max/100)%100;
int maxyear=max/10000;
// keep min For the small number , Then decompose it
int min=day1>day2?day2:day1;
int minday=min%100;
int minmonth=(min/100)%100;
int minyear=min/10000;
// Do not exit the loop until the three values are equal
while((minyear!=maxyear)||(minmonth!=maxmonth)||(minday<=maxday))
{
count++;
minday++;
// Judge whether the number of days exceeds the maximum number of days in the month
if(minday>arr[minmonth])
{
minday=1;
minmonth++;
// Determine whether the number of months exceeds 12
if(minmonth>12)
{
minmonth=1;
minyear++;
}
}
}
cout<<count;
return 0;
}
边栏推荐
- QT控制台打印输出
- Knowledge points of MySQL (7)
- 查看自己电脑连接过的WiFi密码
- 漫画:寻找无序数组的第k大元素(修订版)
- QT console printout
- 漫画:如何实现大整数相乘?(上) 修订版
- Knowing that his daughter was molested, the 35 year old man beat the other party to minor injury level 2, and the court decided not to sue
- Flow characteristics of kitchen knife, ant sword, ice scorpion and Godzilla
- 这个17岁的黑客天才,破解了第一代iPhone!
- 使用QT设计师界面类创建2个界面,通过按键从界面1切换到界面2
猜你喜欢
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
Count the running time of PHP program and set the maximum running time of PHP
Winedt common shortcut key modify shortcut key latex compile button
Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!
MATLAB查阅
提高應用程序性能的7個DevOps實踐
mybash
Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
MySQL之知识点(七)
随机推荐
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
The comprehensive competitiveness of Huawei cloud native containers ranks first in China!
rsync
Seven Devops practices to improve application performance
Cmake tutorial step5 (add system self-test)
Cmake tutorial Step3 (requirements for adding libraries)
QT console printout
Oracle recovery tools -- Oracle database recovery tool
ELK日志分析系统
Domain name resolution, reverse domain name resolution nbtstat
证券网上开户安全吗?证券融资利率一般是多少?
How to save the trained neural network model (pytorch version)
mybash
leetcode每日一练:旋转数组
如何保存训练好的神经网络模型(pytorch版本)
Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
读libco保存恢复现场汇编代码
MySQL之知识点(六)
Thesis reading_ Chinese NLP_ LTP
每日一练:关于日期的一系列