当前位置:网站首页>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;
}

边栏推荐
- Cmake tutorial step6 (add custom commands and generate files)
- SQL Server(2)
- 漫画:如何实现大整数相乘?(整合版)
- 力扣解法汇总1200-最小绝对差
- Kafaka技术第一课
- 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
- Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
- Humi analysis: the integrated application of industrial Internet identity analysis and enterprise information system
- 独立开发,不失为程序员的一条出路
- Webapp development - Google official tutorial
猜你喜欢

7 pratiques devops pour améliorer la performance des applications

漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)

PMP认证需具备哪些条件啊?费用多少啊?

EPM related

「运维有小邓」用于云应用程序的单点登录解决方案

Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)

网络威胁分析师应该具备的十种能力

求解为啥all(())是True, 而any(())是FALSE?

Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!

服务器配置 jupyter环境
随机推荐
Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!
排错-关于clion not found visual studio 的问题
Teamcenter 消息注册前操作或後操作
Check the WiFi password connected to your computer
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
2022年信息系统管理工程师考试大纲
To solve the problem of "double click PDF file, pop up", please install Evernote program
Cartoon: looking for the k-th element of an unordered array (Revised)
十个顶级自动化和编排工具
“12306” 的架构到底有多牛逼?
漫画:如何实现大整数相乘?(上) 修订版
Mask wearing detection based on yolov3
Troubleshooting - about clip not found Visual Studio
Size_ T is unsigned
Oracle Recovery Tools ----oracle数据库恢复利器
The comprehensive competitiveness of Huawei cloud native containers ranks first in China!
c#图文混合,以二进制方式写入数据库
Kafaka技术第一课
Winedt common shortcut key modify shortcut key latex compile button
Tita performance treasure: how to prepare for the mid year examination?