九度の日期

我发现还是需要写博客,否则,真的会忘。。。

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;
}


还做了一个打印日期的题,但我发现就是不对,原来是输出格式不对啊,让你输出“2000-02-29”的格式,可是直接cout只能输出2000-2-29,我记得C++cout貌似也能输出固定格式的,但是我给忘了怎么用了,查网上代码,发现大家都用printf,这个确实比cout省事多了,先记住好了,谁要是记得cout怎么输出那个格式,记得告诉我哦,谢谢!!!

printf("%04d-%02d-%02d\n",y,i,day); 记住加0!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值