当前位置:网站首页>每日一练:关于日期的一系列
每日一练:关于日期的一系列
2022-07-05 17:17:00 【利刃Cc】
计算日期到天数转换

链接: 计算日期到天数转换
思路1:常规的思路是用while循环从这个月的一号循环到该天,但是其实还有很简单的思路,也就是下面的思路二,还有主要讲的也是思路二。
思路二:用一个数组存放每个月累加起来的天数,如第一个月是31天,第二个月是存放31+28=59天,以此类推,这样子存放是有好处的。
我们求这年到某一天的总天数就是这个月的前一个月,以及这个月以前所有天数累加起来的天数和加上该月的天数!但是别忘了判断这一年是否为闰年以及这一天是否超过了二月(因为还没过二月的话没必要加一天)
代码:
#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;
//让n赋值上从这年到这个月的上个月的总天数再加上现在的天数day
int n = arr[month - 1] + day;
//记得判断是否为闰年
if(month > 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
n++;
cout << n;
return 0;
}
日期累加

链接: 日期累加
思路:因为这道题要求m个测试用例,所以要循环m遍。并用nowday记录当月最大天数,便于后面的判断。
然后用while子循环,将addday的天数累加到day和month上,然后addday–。
每次循环都判断一下当天是不是超过当月最大天数,以及该月是不是超过了12月。
代码:
#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;
//因为有多组测试用例,所以用m来循环
for(int i=0; i<m; i++)
{
cin >> year >> month >> day >> addday;
int nowday = arr[month];//用nowday记录当月最大天数
while(addday > 0)
{
day++;
if((month == 2) && ((year%4==0 && year%100!=0) || (year%400==0)))
{
nowday=29;
}
//判断天数是否大于当月最大天数
if(day > nowday)
{
day = 1;
month++;
//判断月数是否大于12
if(month > 12)
{
month=1;
year++;
}
nowday=arr[month];
}
addday--;
}
//把天和月按格式打印
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;
}
打印日期

链接: 打印日期
思路:先用sday存放一下总天数,然后将sday判断这年是不是闰年,然后用while循环将sday拆成day和month,每次day++,sday–,直到sday等于0。
然后记得判断day是不是达到了该月的最大天数,是的话重置一下,还要判断一下月是不是大于12,是的话也要重置一下。
代码:
#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;
//判断一下是不是闰年
if((year % 4 ==0 && year % 100 != 0) || (year % 400 == 0))
arr[2]=29;
//将总天数转化成月和天
while(sday > 0)
{
day++;
if(day > arr[month])
{
day=1;
month++;
if(month > 12)
{
month=1;
year++;
}
}
sday--;
}
//判断一下天数和月打印的格式
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;
//因为有多组测试用例,所有要重置
arr[2]=28;
month=day=1;
return 0;
}
日期差值

链接: 日期差值
思路:先把两个数的年月日分别用三个变量存起来,保持max部分为大的日期,min为小的日期。
然后开始循环,直到min的年月日等于max的年月日为止,用count来统计他们之间相差的天数。
记得判断天是否超过了当月的天数和月数是否超过12月,有的话重置一下!
代码:
#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;
//保持max为大的那个数,然后分解出来
int max=day1>day2?day1:day2;
int maxday=max%100;
int maxmonth=(max/100)%100;
int maxyear=max/10000;
//保持min为小的那个数,然后分解出来
int min=day1>day2?day2:day1;
int minday=min%100;
int minmonth=(min/100)%100;
int minyear=min/10000;
//直到三个值相等才退出循环
while((minyear!=maxyear)||(minmonth!=maxmonth)||(minday<=maxday))
{
count++;
minday++;
//判断一下天数是否超过当月的最大天数
if(minday>arr[minmonth])
{
minday=1;
minmonth++;
//判断一下月数是否超过12
if(minmonth>12)
{
minmonth=1;
minyear++;
}
}
}
cout<<count;
return 0;
}

边栏推荐
- Is it safe and reliable to open futures accounts on koufu.com? How to distinguish whether the platform is safe?
- 独立开发,不失为程序员的一条出路
- C (WinForm) the current thread is not in a single threaded unit, so ActiveX controls cannot be instantiated
- How to write a full score project document | acquisition technology
- stirring! 2022 open atom global open source summit registration is hot!
- Oracle Recovery Tools ----oracle数据库恢复利器
- Cartoon: looking for the k-th element of an unordered array (Revised)
- Force deduction solution summary 1200 minimum absolute difference
- Cartoon: interesting [pirate] question
- Seven Devops practices to improve application performance
猜你喜欢

C # mixed graphics and text, written to the database in binary mode

7 pratiques devops pour améliorer la performance des applications
Tips for extracting JSON fields from MySQL

7. Scala class

提高应用程序性能的7个DevOps实践

Machine learning 02: model evaluation

Winedt common shortcut key modify shortcut key latex compile button

北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员

33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
Example tutorial of SQL deduplication
随机推荐
关于mysql中的json解析函数JSON_EXTRACT
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
Tips for extracting JSON fields from MySQL
The comprehensive competitiveness of Huawei cloud native containers ranks first in China!
Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
mysql如何使用JSON_EXTRACT()取json值
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
Redis+caffeine two-level cache enables smooth access speed
C # mixed graphics and text, written to the database in binary mode
读libco保存恢复现场汇编代码
Is it safe for China Galaxy Securities to open an account? How long can I buy stocks after opening an account
Cartoon: a bloody case caused by a math problem
机器学习01:绪论
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
thinkphp3.2.3
数据访问 - EntityFramework集成
Understand the usage of functions and methods in go language
Rider 设置选中单词侧边高亮,去除警告建议高亮
外盘黄金哪个平台正规安全,怎么辨别?
Cartoon: looking for the k-th element of an unordered array (Revised)