我发现还是需要写博客,否则,真的会忘。。。
1.三维数组,年月日表示数组下标,将日期本身与存储地址相关 hash
2. C中用到%4d来读取整数前4位,C++中,我就用了%和/,但要注意,若想取整数后两位,应该用%100,
3.当需要开辟大量内存空间时,需要定义为全局变量,比如buf[5001][13][32],或者在函数中动态申请变量空间!
附上日期常用的处理方法!预处理!!!
思路就是定义一个结构体Date,计算出nextday,在主函数中调用函数计算出所有日期距离0年1月1日的天数,接着所有问题就都解决啦!`(*∩_∩*)′
宏定义表示是否闰年!
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
#define ISYEAR(x) x%100!=0&&x%4==0||x%400==0?1:0
int dayOfMonth[13][2]={
0,0,
31,31,
28,29,
31,31,
30,30,
31,31,
30,30,
31,31,
31,31,
30,30,
31,31,
30,30,
31,31
};
struct Date{
int day;
int month;
int year;
void nextDay(){
day++;
if(day>dayOfMonth[month][ISYEAR(year)]){
day=1;
month++;
if(month>12){
month=1;
year++;
}
}
}
};
int buf[3001][13][32];
char monthName[13][20]={
"",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
char weekName[7][20]={
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
int main(){
Date tmp;
int cnt=0;
tmp.day=1;
tmp.month=1;
tmp.year=0;
while(tmp.year!=3001){
buf[tmp.year][tmp.month][tmp.day]=cnt;
tmp.nextDay();
cnt++;
}
int d1,m1,y1;
char s[20];
while(cin>>d1>>s>>y1){
for(int i=1;i<13;i++)
if(strcmp(s,monthName[i])==0)
m1=i;
int w=((buf[y1][m1][d1]-buf[2016][06][20]+1)%7+7)%7;
cout<<weekName[w]<<endl;
}
return 0;
}
printf("%04d-%02d-%02d\n",y,i,day); 记住加0!!!