当前位置:网站首页>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;
}
边栏推荐
- Anaconda中配置PyTorch环境——win10系统(小白包会)
- MySQL之知识点(六)
- Interpretation: how to deal with the current security problems faced by the Internet of things?
- Flow characteristics of kitchen knife, ant sword, ice scorpion and Godzilla
- WebApp开发-Google官方教程
- Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
- Oracle recovery tools -- Oracle database recovery tool
- ITK Example
- Mysql5.6 parsing JSON strings (supporting complex nested formats)
- 如何保存训练好的神经网络模型(pytorch版本)
猜你喜欢
2022新版PMP考试有哪些变化?
MATLAB查阅
LeetCode 练习——206. 反转链表
Short the command line via jar manifest or via a classpath file and rerun
IDEA 项目启动报错 Shorten the command line via JAR manifest or via a classpath file and rerun.
Thesis reading_ Chinese NLP_ LTP
ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
查看自己电脑连接过的WiFi密码
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
Cmake tutorial Step2 (add Library)
随机推荐
Disabling and enabling inspections pycharm
蚂蚁金服的暴富还未开始,Zoom的神话却仍在继续!
Cartoon: a bloody case caused by a math problem
rsync
Cmake tutorial Step3 (requirements for adding libraries)
Redis+caffeine two-level cache enables smooth access speed
Why is all (()) true and any (()) false?
Humi analysis: the integrated application of industrial Internet identity analysis and enterprise information system
ELK日志分析系统
Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!
Is it safe to open an account online? What is the general interest rate of securities financing?
Force deduction solution summary 729- my schedule I
Size_t 是无符号的
查看自己电脑连接过的WiFi密码
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Anaconda中配置PyTorch环境——win10系统(小白包会)
leetcode每日一题:字符串中的第一个唯一字符
漫画:如何实现大整数相乘?(整合版)
解决“双击pdf文件,弹出”请安装evernote程序
Cmake tutorial step6 (add custom commands and generate files)