Regarding the date, the main thing is to calculate the number of days. The form will change, but it is still inseparable. I will record two questions about the date class, and the main function remains unchanged.
Summarize:
int cal (int y, int m, int d) { int days=d; bool isprime=0; int a[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}; if(y%4==0&&y%100!=0||y%400==0) print = 1; for(int i=0;i<m;i++) { days+=a[isprime][i]; } return days; }
1. Gaussian Diary
The great mathematician Gauss had a good habit: keep a diary anyway.
There is a difference in his diary. He never indicates the year, month and day, but instead uses an integer, such as: 4210
It was later known that that integer was the date, which represented the day after Gauss was born. This may also be a good habit. It reminds the owner all the time: the day goes by, how much time can be wasted?
Gauss was born: April 30, 1777. In the diary of an important theorem discovered by Gauss, it is marked: 5343, so it can be calculated that the day is: December 15, 1791.
The diary on the day that Gauss received his Ph.D. was marked: 8113 Please calculate the year, month, and day of Gauss's Ph.D.
The format of the submitted answer is: yyyy-mm-dd, for example: 1980-03-21
Analysis: This question is given a date and a number of days, to calculate
Below is the code to solve this problem
#include<iostream>
using namespace std;
int m[2][12]={//month corresponding to leap year
31,29,31,30,31,30,31,31,30,31,30,31,
31,28,31,30,31,30,31,31,30,31,30,31
};
bool judge(int n)//Judge whether it is a leap year
{
if(n%400==0||(n%4==0&&n%100!=0)) return 1;
return 0;
}
intmain()
{
int n;
while(cin>>n)
{
int year=1777,month=4,day=30;
while(--n)
{
day++; //simulate the number of days
if(day>m[!judge(year)][month-1])//If the number of days is greater than the number of days corresponding to the current month, the month plus one
{
month++;
if(month>12)//If the month is greater than 12, add one to the year, and clear the month to one
{
year++;
month=1;
}
day=1;//The number of days is cleared into one
}
}
cout<<year<<"--"<<month<<"--"<<day<<endl;
}
return 0;
}
2.
In 1949, National Day (October 1) was a Saturday.
This year (2012) the National Day is Monday.So, from the founding of the People's Republic to the present, how many times has National Day happened to be a Sunday?
#include <iostream> using namespace std; int cal (int y, int m, int d) { int days=d; bool isprime=0; int a[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}; if(y%4==0&&y%100!=0||y%400==0) print = 1; for(int i=0;i<m;i++) { days+=a[isprime][i]; } return days; } intmain() { int total=cal(1949,12,31)-cal(1949,10,2);//Note that the title says that October 1, 1949 is Saturday, indicating that October 2 is Sunday for(int i=1950;i<2012;i++) { total+=cal(i,10,1)-cal(i,1,1)+1;//This +1 is number 1.1 and counts as one day if((total)%7==0) { cout<<i<<endl; } } return 0; }