算法比赛日期规划 date【考察字符串和日期的操作】

题目描述

在这里插入图片描述在这里插入图片描述

ac代码:

#include <bits/stdc++.h>

using namespace std;


typedef long long ll;

void getDate(int &year,int &month,int &day,string s){
	int flag = 0;
	for(int i = 0;i < s.length();i++){
		if(flag == 0&&s[i]!='-'){
			year = year*10 + (s[i]-'0');
		}else if(flag==1 && s[i]!='-'){
			month = month*10+(s[i]-'0');
		}else if(flag == 2&& s[i]!='-'){
			day = day*10+(s[i]-'0');
		}
		if(s[i]=='-'){
			flag++;
		}
	}
	
} 

void getTime(int &h,int &m,int &second,string s){
	int flag = 0;
	for(int i = 0;i < s.length();i++){
		if(flag == 0&&s[i]!=':'){
			h = h*10 + (s[i]-'0');
		}else if(flag==1 && s[i]!=':'){
			m = m*10+(s[i]-'0');
		}else if(flag == 2&& s[i]!=':'){
			second = second*10+(s[i]-'0');
		}
		if(s[i]==':'){
			flag++;
		}
	}
}

bool judgeYear(int year){
	if(year%4==0 && year%100!=0 || year%400==0){
        return true;
    }
    else{
        return false;
    }
}

int getMonthToDay(int year,int month,bool flag){
	int day = 0;
	if(month == 1){
		return 0;		
	}else if(month == 2){
		return 31;
	}else{
		if(month == 3){
			day = 59;
		}else if(month == 4){
			day = 90;
		}else if(month == 5){
			day = 120;
		}else if(month == 6){
			day = 151;
		}else if(month == 7){
			day = 181;
		}else if(month == 8){
			day = 212;
		}else if(month == 9){
			day = 243;
		}else if(month == 10){
			day = 273;
		}else if(month == 11){
			day = 304;
		}else if(month == 12){
			day = 334;
		}
		if(flag){
			day++;
		}
		return day;
	}
}

int main(){
	
	string data1;
	string time1;
	string data2;
	string time2;
	int year1,month1,day1,hour1,min1,second1;
	int year2,month2,day2,hour2,min2,second2;
	cin>>data1>>time1>>data2>>time2;
	year1 = 0;
	month1=0;
	day1=0;
	hour1=0;
	min1=0;
	second1=0;
	year2 = 0;
	month2=0;
	day2=0;
	hour2=0;
	min2=0;
	second2=0;
	
	ll differYear = 0;
	ll differMonth = 0;
	ll differDay = 0;

	getDate(year1,month1,day1,data1);
	getDate(year2,month2,day2,data2);
	getTime(hour1,min1,second1,time1);
	getTime(hour2,min2,second2,time2);
	differYear = year2-year1;
	bool Flag1 = judgeYear(year1);
	bool Flag2 = judgeYear(year2);
	if(differYear!=0){
		for(int i = year1;i < year2;i++){
			if(judgeYear(i)){
				differDay += 366;
			}else{
				differDay += 365;
			}
		}
	}
	int tempday1;
	int tempday2;
	tempday1 = getMonthToDay(year1,month1,Flag1)+day1;
	tempday2 = getMonthToDay(year2,month2,Flag2)+day2;

	
	differDay += (tempday2-tempday1);
	

	
	ll differSecond;
	int tempsecond1;
	int tempsecond2;
	ll ans;
	tempsecond1 = hour1*60*60+min1*60+second1;
	tempsecond2 = hour2*60*60+min2*60+second2;
	
	differSecond = differDay*24*60*60+tempsecond2-tempsecond1;
	ans = differSecond*1000;

	cout<<ans<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值