当前位置:网站首页>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)
- 漫画:如何实现大整数相乘?(整合版)
- Flask solves the problem of CORS err
- Interpretation: how to deal with the current security problems faced by the Internet of things?
- 网络威胁分析师应该具备的十种能力
- Cmake tutorial step5 (add system self-test)
- MySQL之知识点(七)
- Operation before or after Teamcenter message registration
- 读libco保存恢复现场汇编代码
- Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
猜你喜欢
Kafaka技术第一课
Why is all (()) true and any (()) false?
解决“双击pdf文件,弹出”请安装evernote程序
Redis+caffeine two-level cache enables smooth access speed
Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
ELK日志分析系统
Anaconda中配置PyTorch环境——win10系统(小白包会)
Ten top automation and orchestration tools
Mask wearing detection based on yolov3
Cmake tutorial Step4 (installation and testing)
随机推荐
网络威胁分析师应该具备的十种能力
Cmake tutorial Step2 (add Library)
Why is February 28 in the Gregorian calendar
Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
Cartoon: how to multiply large integers? (next)
tkinter窗口预加载
删除数组中的某几个元素
C # mixed graphics and text, written to the database in binary mode
QT控制台打印输出
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
Teamcenter 消息注册前操作或后操作
Cmake tutorial Step3 (requirements for adding libraries)
c#图文混合,以二进制方式写入数据库
力扣解法汇总1200-最小绝对差
Interpretation: how to deal with the current security problems faced by the Internet of things?
Redis基础
提高应用程序性能的7个DevOps实践
Mysql5.6 parsing JSON strings (supporting complex nested formats)
Is it safe for China Galaxy Securities to open an account? How long can I buy stocks after opening an account