文章

61

粉丝

137

获赞

18

访问

40.0k

头像
日期差值 题解:c++实现

#include<bits/stdc++.h>
using namespace std;

//判断是否是闰年
bool judge(int year){
    if(year % 4 == 0 ){
        if(year % 100 == 0){
			if(year % 400 == 0)
				return true;
			else	
				return false;
		} 
    }else
       return false ;
}

int main(){
    string s1 ,s2;
    int year1 ,month1 ,day1;
    int year2 ,month2 ,day2;
    int num = 0;
    int months[] = {0 ,31 ,28 ,31 ,30 ,31 ,30 ,31 ,31 ,30 ,31 ,30 ,31} ;
    scanf("%4d%2d%2d" ,&year1 ,&month1 ,&day1) ;
	scanf("%4d%2d%2d" ,&year2 ,&month2 ,&day2) ;
       
	//case1: 两个日期在同一年中
	if(year1 == year2){
		if(judge(year1))
			months[2] = 29;     //闰年替换2月天数
		else months [2] = 28 ;
		for(int i = month1 + 1 ;i < month2 ;i++)
			num += months[i];
		if(month1 == month2)   num = day2 - day1 + 1;
		else num += (months[month1] - day1+ day2 + 1);

		cout << num << endl;
		return 0;
	}

	//case 2: 两个日期不在同一年中
	for(int i = year1 + 1 ;i < year2 ;i++){
		i...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发