当前位置:网站首页>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;
}
边栏推荐
- Customize the theme of matrix (I) night mode
- 每日一练:关于日期的一系列
- 「运维有小邓」用于云应用程序的单点登录解决方案
- 提高應用程序性能的7個DevOps實踐
- 一口气读懂 IT发展史
- Count the running time of PHP program and set the maximum running time of PHP
- ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
- EPM相关
- ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
- MySQL之知识点(七)
猜你喜欢
提高應用程序性能的7個DevOps實踐
2022新版PMP考试有哪些变化?
leetcode每日一练:旋转数组
Ten top automation and orchestration tools
Abnormal recovery of virtual machine Oracle -- Xi Fenfei
Matlab reference
Knowledge points of MySQL (6)
论文阅读_医疗NLP模型_ EMBERT
ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée
漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
随机推荐
Cartoon: a bloody case caused by a math problem
ELK日志分析系统
Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
Force deduction solution summary 1200 minimum absolute difference
ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
漫画:有趣的海盗问题 (完整版)
c#图文混合,以二进制方式写入数据库
Interpretation: how to deal with the current security problems faced by the Internet of things?
这个17岁的黑客天才,破解了第一代iPhone!
BigDecimal除法的精度问题
Mongodb (quick start) (I)
Thesis reading_ Medical NLP model_ EMBERT
普通程序员看代码,顶级程序员看趋势
Oracle recovery tools -- Oracle database recovery tool
Domain name resolution, reverse domain name resolution nbtstat
C # mixed graphics and text, written to the database in binary mode
MySQL之知识点(七)
Seven Devops practices to improve application performance
Ordinary programmers look at the code, and top programmers look at the trend
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声