文章

84

粉丝

2

获赞

560

访问

22.0k

头像
日期差值 题解:
P1290 上海交通大学/西北工业大学机试题
发布于2026年3月16日 16:38
阅读数 30

#include<bits/stdc++.h>

using namespace std;

bool is_year(int y) {
	return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}


int main() {
	
	string s1, s2;
	while (cin >> s1 >> s2) {
		
		int month[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		
		int y1 = atoi(s1.substr(0, 4).c_str()), y2 = atoi(s2.substr(0, 4).c_str());
		int m1 = atoi(s1.substr(4, 2).c_str()), m2 = atoi(s2.substr(4, 2).c_str());
		int d1 = atoi(s1.substr(6, 2).c_str()), d2 = atoi(s2.substr(6, 2).c_str());
		
		if (is_year(y1)) month[2] = 29;
		
		int sum = 1;
		while (y1 < y2 || m1 < m2 || d1 < d2) {
			sum ++ ;
			d1 ++ ;
			if (d1 > month[m1]) {
				d1 = 1;
				m1 ++ ;
			}
			if (m1 > 12) {
				y1 ++ ;
				if (is_year(y1)) {
					month[2] = 29;
				} else {
					month[2] = 28;
				}
				m1 = 1;
			}
		}
		cout << sum << endl;
		
	}
	
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发