文章

18

粉丝

0

获赞

3

访问

11.1k

头像
日期差值 题解:自用笔记(C语言)

#include<stdio.h>
int days[12] = { 31,0,31,30,31,30,31,31,30,31,30,31 };
int isleapyear(int year) {
	if (year % 400 == 0) return 1;
	else if (year % 4 == 0 && year % 100) return 1;
	else return 0;
}

int countdate(int x, int start) {
	int year, month, day, k, sum = 0;
	//数据转化为日期,不断取个位
	day = x % 10;
	x /= 10;
	day += x % 10 * 10;
	x /= 10;
	month = x % 10;
	x /= 10;
	month += x % 10 * 10;
	x /= 10;
	year = x;
    //输出测试
	//printf("%d-%d-%d\n", year, month, day);
	
	//计算天数
	if (isleapyear(year))
		days[1] = 29;
	else
		days[1] = 28;
	for (int i = 0; i < month - 1; i++) {
		sum += days[i];
	}
	sum += day;
	
	//跨年
	int m = year - start;//间隔多少年
	for (int i = 1; i <= m; i++) {
		if (isleapyear(start))
			k = 366;
		else
			k = 365;
		sum += i * k;
		start++;
	}
	return sum;
}
int main() {
	int x, y;
	while(scanf("%d\n%d", &x, &y)!=EOF){
	int y1 = x / 10000, y2 = y / 10000;
	int start = y1 > y2 ? y2 : y1;//找...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发