当前位置:网站首页>Blue Bridge Cup provincial match training camp - Calculation of date
Blue Bridge Cup provincial match training camp - Calculation of date
2022-07-24 11:08:00 【practical_ sharp】
Calculation of date
People often ask you what month and what day of the week it is , How not to check the calendar , Calculate it directly with the program ? The simplest way is , Remember the day of the week a long time ago , For example, A.D 1 year 1 month 1 It's Monday . Then simulate day by day , Calculate the day of the week . This method is easy to understand , But the implementation code may be long . besides , There is a formula that can quickly calculate the day of the week according to the date , This is known as Chuckim Larson formula
Suppose the week is w, Year is y, Month is m, The date is d:
w=(d+2×m+3×(m+1)/5+y+y/4−y/100+y/400)%7
Then put the calculated w add 1 It's the real day of the week .
Pay attention to the annual 1,2 The month should be regarded as the previous year 13,14 Monthly calculation , All the above division methods are integer division
Garlic King's birthday
Garlic King's birthday is coming , I hope it's on the weekend , Garlic gentleman asks you to help figure out the day of his birthday .

Code
#include <iostream>
#include <string>
using namespace std;
string weekdays[7] = {
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int whatday(int y, int m, int d)
{
m == 1 && (m = 13, y -= 1);
m == 2 && (m = 14, y -= 1);
return (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
}
int main() {
int y, m, d;
cin >> y >> m >> d;
cout << weekdays[whatday(y, m, d)] << endl;
return 0;
}
You are in love with sister Huaye
You are in love with sister Huaye . Bless them . Garlic king wants to know their number 100 God ,200 God … On the anniversary of .
Input format
Input format Input 4 It's an integer y,m,d,k Indicates the date they were together , The guarantee is one by one 1900 year 1 month 1 Date after , Garlic king wants to know their k(0≤k≤10000) Day of remembrance .

Code
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
// Date structure
typedef struct
{
int y;
int m;
int d;
}day;
int m[2][13] = {
{
0,31,28,31,30,31,30,31,31,30,31,30,31}, {
0,31,29,31,30,31,30,31,31,30,31,30,31}};
// Determine if it's a leap year
int isrunyear(int year)
{
if(year %400 ==0 || (year%100!=0 && year%4 ==0))
return 1;
else
return 0;
}
// obtain n Days after
day getDate(day p,int n)
{
while(n)
{
// If the month is not full , Days ++
if( p.d < m[isrunyear(p.y)][p.m])
{
p.d++;
}
// If the days of the month are full and the month is not 12 month
else if(p.m !=12 && p.d == m[isrunyear(p.y)][p.m])
{
p.d = 1;
p.m++;
}
// If it is the last day of the year
else if(p.m == 12 && p.d == 31)
{
p.y++;
p.m = 1;
p.d = 1;
}
n--;// Time goes forward for a day ,n Self reduction
}
return p;
}
int main()
{
day p;
int n;
cin>>p.y>>p.m>>p.d>>n;
p = getDate(p,n);
cout<<p.y<<"-";
p.m<10 && cout<<0;
cout<<p.m<<"-";
p.d<10 && cout<<0;
cout<<p.d<<endl;
return 0;
}
The holiday season
Calendar has The solar calendar ( Gregorian calendar ) and Lunar calendar ( Lunar calendar ) Points . There are legal holidays every year , These fall into three categories — Weekends 、 Gregorian holidays 、 Lunar holidays .
Weekends
1) Saturday and Sunday 2 God
Gregorian holidays
1) New year's Day : Every year in the Gregorian calendar 1 month 1 Japan , Have a holiday 1 God
2) labor day : Every year in the Gregorian calendar 5 month 1 Japan , Have a holiday 1 God
3) National Day : Every year in the Gregorian calendar 10 month 1 Japan , Have a holiday 3 God
4) Christmas : Every year in the Gregorian calendar 12 month 25 Japan , Have a holiday 1 God
Lunar holidays
1) Spring Festival : Every year in the lunar calendar 1 month 1 Japan , Have a holiday 3 God
2) Qingming Festival : Every year in the Gregorian calendar 4 month 4 - 6 One day between the days , Have a holiday 1 God
3) The Dragon Boat Festival : Every year in the lunar calendar 5 month 5 Japan , Have a holiday 1 God
4) Mid-Autumn Festival : Every year in the lunar calendar 8 month 15 Japan , Have a holiday 1 God
When holidays coincide with weekends , Weekends No delay also Not in advance , Ensure that holidays will not coincide . Now I give you all the lunar holidays of a certain year The solar calendar date , And then 1 month 1 What day is the day of the week , Please calculate this year ( The solar calendar 1 month 1 The day is coming 12 month 31 Japan ) How many days off did you have ( Including weekends 、 Gregorian and lunar holidays ).
Input format
Enter the year on the first line y(1900<y≤2050).
Next 4 That's ok , Enter two integers per line ,m,d, It means Spring Festival 、 Qingming Festival 、 The date of the Dragon Boat Festival and the Mid Autumn Festival in the Gregorian calendar .
An integer in the last line represents the year 1 month 1 What day is the day of the week ( The day of the week , Count every week from Monday , Monday is the first day ).
Output format
Output an integer , Indicates the number of days off in the current year .
The sample input
2017
1 28
4 4
5 30
10 4
7
Sample output
113
Their thinking
Define an array days[], Each element in it represents every day of the year , Initially, all elements are 0.
Traversing an array , First put the lunar calendar , Set on the day of the Gregorian Festival 1.
Then set all Saturdays and Sundays 1.
Finally, days Sum the array and output it .
Code
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int days[367];
// Determine if it's a leap year
int isrunnian(int n)
{
if(n%400==0 || (n%100!=0 && n%4==0))
return 1;
else
return 0;
}
// Calculate the total number of days in the year
int alldays(int n)
{
if(isrunnian(n))
return 366;
else
return 365;
}
// Calculate the day of the year
int getsnumday(int year,int m,int d)
{
int mm[2][14] = {
{
0,31,28,31,30,31,30,31,31,30,31,30,31},{
0,31,29,31,30,31,30,31,31,30,31,30,31}};
int ret = 0;
for(int i=1;i<=m-1;i++)
ret += mm[isrunnian(year)][i];
ret += d;
return ret;
}
int main()
{
int year;
cin>>year;
// Enter four festivals
int x1,y1,x2,y2,x3,y3,x4,y4;
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
int start;// The first day of the week
cin>>start;
int n1,n2;
switch(start)
{
case 1:n1=6;n2=7;break;// The first Saturday and Sunday of that year was 6,7
case 2:n1=5;n2=6;break;// The first Saturday and Sunday of that year was 5,6
case 3:n1=4;n2=5;break;// The first Saturday and Sunday of that year was 4,5
case 4:n1=3;n2=4;break;// The first Saturday and Sunday of that year was 3,4
case 5:n1=2;n2=3;break;// The first Saturday and Sunday of that year was 2,3
case 6:n1=1;n2=2;break;// The first Saturday and Sunday of that year was 1,2
case 7:n1=1;n2=7;break;// The first Saturday and Sunday of that year was 1,7
}
// Let's start with the Gregorian calendar holiday days[] Set as 1
days[1] = 1;// New year's Day 1 month 1 Japan
days[getsnumday(year,5,1)] = 1;// labor day ,5 month 1 Japan
days[getsnumday(year,10,1)] = days[getsnumday(year,10,2)] = days[getsnumday(year,10,3)] = 1;// National Day , Have a holiday 3 God
days[getsnumday(year,12,25)] = 1;// Christmas holiday 1 God
// Lunar holidays
days[getsnumday(year,x1,y1)] = days[getsnumday(year,x1,y1+1)] = days[getsnumday(year,x1,y1+2)] = 1;// Spring Festival 3 God
days[getsnumday(year,x2,y2)] = 1;// Tomb Sweeping Day holiday 1 God
days[getsnumday(year,x3,y3)] = 1;// The Dragon Boat Festival is a holiday 1 God
days[getsnumday(year,x4,y4)] = 1;// Mid Autumn Festival holiday 1 God
// Saturday, Sunday
for(int i=1;i<=alldays(year);i++)
if(i%7 == n1%7 || i%7 == n2%7)
days[i] = 1;
// Count the number of house price days
int sum =0;
for(int i=1;i<=alldays(year);i++)
sum += days[i];
cout<<sum;
return 0;
}
边栏推荐
- MySQL paging
- 数据可视化-《白蛇2:青蛇劫起》(1)
- [FPGA]: frequency measurement
- [white hat talks about web security] Chapter 1 my security world view
- CSDN blog removes the uploaded image watermark
- Docker installs 3 master and 3 slave redis clusters
- [class, abstraction and inheritance]
- Reptiles and counter crawls: an endless battle
- 乘势而上,OceanBase推动数字支付精益增长
- Research on parameter setting of MATLAB FFT
猜你喜欢

Analysis of Lagrange multiplier method and its duality

自学软件测试天赋异禀——不是盖的

Simply understand MODBUS function code and partition

浅析拉格朗日乘数法及其对偶问题

小熊派学习——内核开发

这个应该是全网最全的接口测试工具之postman

Working principle and function application of frequency converter

Only "a little bit", why do developers look up to you?

轻松读懂三极管,原来它是这样工作的

The difference between Lora wireless technology and lorawan gateway module
随机推荐
UNIX C language POSIX thread creation, obtaining thread ID, merging thread, separating thread, terminating thread, thread comparison
Ldr6028 charging OTG live line live sound card audio adapter is the most cost-effective solution
Docker installs 3 master and 3 slave redis clusters
Analysis of Lagrange multiplier method and its duality
Only "a little bit", why do developers look up to you?
Zero basic learning canoe panel (4) -- button
2018 arXiv | Objective-Reinforced Generative Adversarial Networks (ORGAN) for Sequence Generation Mo
周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
[ISE] development process plus bit, burning of MCS files
[FPGA]: IP core DDS
MySQL paging
STM32+ESP8266+MQTT协议连接阿里云物联网平台
MySQL根据备注查询表、字段
Zero basic learning canoe panel (7) -- file selection (pathdiaglog)
UNIX C language POSIX mutex thread synchronization
[interview: Basics 04: insert order]
[FPGA]: IP core - multiplier
Siemens 200smart self created library and description
轻松读懂三极管,原来它是这样工作的
Development and course of Bluetooth Technology