码迷,mamicode.com
首页 > 其他好文 > 详细

Sicily 1814. 日期计算问题

时间:2015-04-28 16:16:09      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

【转自网络大神,由本人整理】

Description

试用 C++的类来表示日期,给定 2 个日期 yyyy.mm.dd 求两个日期间相差的天数。

 

Input

第 1 行为一个正整数T,表示测试数。

对于每个测试点,第 1 行与第 2 行分别有两个日期 yyyy.mm.dd。

 

Output

对于每个测试点,输出一行数字,表示相差的天数。

Sample Input
技术分享 Copy sample input to clipboard
2
2000.02.28
2000.03.01
6297.01.21
2351.11.27
Sample Output
2
1440938

  • 计算闰年:能被4整除且不能被100整除的是闰年; 能被400整除的是闰年
  • 计算出起始日期是该年的哪一天day1
  • 计算出终止日期是该年的哪一天day2
  • 计算出两个年份之间的天数,包括起始的年份,不包括终止的年份
  • 用上一步的天数减去起始日期的所在年的day1,加上终止日期所在年的day2即为所求答案
方法一:【来自@chenhq1991

#include<iostream>
#include<string>
using namespace std;

int DaysOfYear(int year)
{
	if((year % 4 == 0 && year % 100 != 0) ||(year % 400 == 0))
	{
		return 366;//闰年
	}
	else
	{
		return 365;
	}
}

int DaysOfMonth(int year,int month)
{
	switch(month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		return 31;
	case 2:
		if((year % 4 == 0 && year % 100 != 0) ||(year % 400 == 0))
			return 29;
		else
			return 28;
	case 4:
	case 6:
	case 9:
	case 11:
		return 30;
	default:
		return 0;
	}
}
class Date{
public:
	Date(string date)
	{
		year = month = day = 0;
		int firstDot = date.find('.',0);//找到年后的那个点
		int secondDot = date.find('.', firstDot + 1);//找到月后的那个点
		//处理day
		int counter = 0;
		for(int i = date.length() - 1; i > secondDot; i--)
		{
			char d = date[i];
			int temp = d - 48;
			for(int j = 0; j < counter; j++)
			{
				temp = temp * 10;
			}
			day = day + temp;
			counter++;	
		}
		counter = 0;
		//处理month
		for(int i = secondDot - 1; i > firstDot; i--)
		{
			char m = date[i];
			int temp = m - 48;
			for(int j = 0; j < counter; j++)
			{
				temp = temp * 10;
			}
			month = month + temp;
			counter++;
		}
		counter = 0;
		//处理year
		for(int i = firstDot - 1; i >= 0; i--)
		{
			char y = date[i];
			int temp = y - 48;
			for(int j = 0; j < counter; j++)
			{
				temp = temp * 10;
			}
			year = year + temp;
			counter++;
		}
	}
	int GetYear()
	{
		return year;
	}

	int GetMonth()
	{
		return month;
	}

	int GetDay()
	{
		return day;
	}
	//重载‘-’
	//要求left 的时间晚于 right的
	friend int operator - (Date left, Date right)
	{
		int daysBeforeRight = 0;
		//先计算出右边的日期是一年中的哪一天
		for(int i = 1; i < right.GetMonth(); i++)
		{
			daysBeforeRight = daysBeforeRight + DaysOfMonth(right.GetYear(), i);
		}
		daysBeforeRight = daysBeforeRight + right.GetDay();//如果是2000.1.1,daysBeforeRight 就是1
		int daysBeforeLeft = 0;
		//计算出左边的日期是一年中的哪一天
		for(int i = 1; i < left.GetMonth(); i++)
		{
			daysBeforeLeft = daysBeforeLeft + DaysOfMonth(left.GetYear(), i);
		}
		daysBeforeLeft = daysBeforeLeft + left.GetDay();
		int interval = 0;//两个日期的间隔
		for(int i = right.GetYear(); i < left.GetYear(); i++)
		{
			interval = interval + DaysOfYear(i);
		}
		//实际的间隔
		interval = interval - daysBeforeRight + daysBeforeLeft;
		return interval;
	}
private:
	int year;
	int month;
	int day;
};

int main()
{
	int caseNum;
	cin >> caseNum;
	while(caseNum--)
	{
		string first, second;
		cin >> first >> second;
		Date one(first);
		Date two(second);
		//比较两个时间的先后
		if(first.length() > second.length())//长度长的时间较晚
		{
			int interval = one - two;
			cout << interval << endl;
		}
		else
		{
			if(first.length() < second.length())
			{
				int interval = two - one;
				cout << interval << endl;
			}
			else//长度相等
			{
				if(first > second)//字符串比较大小,大的时间较晚
				{
					int interval = one - two;
					cout << interval << endl;
				}
				else
				{
					int interval = two - one;
					cout << interval << endl;
				}
			}
		}
		
	}
	return 0;
}

方法二:【来自@绿夜

#include<iostream>
#include<string>
using namespace std;

int DaysOfYear(int year)
{
	if((year % 4 == 0 && year % 100 != 0) ||(year % 400 == 0))
	{
		return 366;//闰年
	}
	else
	{
		return 365;
	}
}

int DaysOfMonth(int year,int month)
{
	switch(month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		return 31;
	case 2:
		if((year % 4 == 0 && year % 100 != 0) ||(year % 400 == 0))
			return 29;
		else
			return 28;
	case 4:
	case 6:
	case 9:
	case 11:
		return 30;
	default:
		return 0;
	}
}
class Date{
public:
	Date(string date)
	{
		year = month = day = 0;
		int firstDot = date.find('.',0);//找到年后的那个点
		int secondDot = date.find('.', firstDot + 1);//找到月后的那个点
		//处理day
		int counter = 0;
		for(int i = date.length() - 1; i > secondDot; i--)
		{
			char d = date[i];
			int temp = d - 48;
			for(int j = 0; j < counter; j++)
			{
				temp = temp * 10;
			}
			day = day + temp;
			counter++;	
		}
		counter = 0;
		//处理month
		for(int i = secondDot - 1; i > firstDot; i--)
		{
			char m = date[i];
			int temp = m - 48;
			for(int j = 0; j < counter; j++)
			{
				temp = temp * 10;
			}
			month = month + temp;
			counter++;
		}
		counter = 0;
		//处理year
		for(int i = firstDot - 1; i >= 0; i--)
		{
			char y = date[i];
			int temp = y - 48;
			for(int j = 0; j < counter; j++)
			{
				temp = temp * 10;
			}
			year = year + temp;
			counter++;
		}
	}
	int GetYear()
	{
		return year;
	}

	int GetMonth()
	{
		return month;
	}

	int GetDay()
	{
		return day;
	}
	//重载‘-’
	//要求left 的时间晚于 right的
	friend int operator - (Date left, Date right)
	{
		int daysBeforeRight = 0;
		//先计算出右边的日期是一年中的哪一天
		for(int i = 1; i < right.GetMonth(); i++)
		{
			daysBeforeRight = daysBeforeRight + DaysOfMonth(right.GetYear(), i);
		}
		daysBeforeRight = daysBeforeRight + right.GetDay();//如果是2000.1.1,daysBeforeRight 就是1
		int daysBeforeLeft = 0;
		//计算出左边的日期是一年中的哪一天
		for(int i = 1; i < left.GetMonth(); i++)
		{
			daysBeforeLeft = daysBeforeLeft + DaysOfMonth(left.GetYear(), i);
		}
		daysBeforeLeft = daysBeforeLeft + left.GetDay();
		int interval = 0;//两个日期的间隔
		for(int i = right.GetYear(); i < left.GetYear(); i++)
		{
			interval = interval + DaysOfYear(i);
		}
		//实际的间隔
		interval = interval - daysBeforeRight + daysBeforeLeft;
		return interval;
	}
private:
	int year;
	int month;
	int day;
};

int main()
{
	int caseNum;
	cin >> caseNum;
	while(caseNum--)
	{
		string first, second;
		cin >> first >> second;
		Date one(first);
		Date two(second);
		//比较两个时间的先后
		if(first.length() > second.length())//长度长的时间较晚
		{
			int interval = one - two;
			cout << interval << endl;
		}
		else
		{
			if(first.length() < second.length())
			{
				int interval = two - one;
				cout << interval << endl;
			}
			else//长度相等
			{
				if(first > second)//字符串比较大小,大的时间较晚
				{
					int interval = one - two;
					cout << interval << endl;
				}
				else
				{
					int interval = two - one;
					cout << interval << endl;
				}
			}
		}
		
	}
	return 0;
}

自己的,基本按照大神的思路

#include<iostream>
#include<cstdio>
#include<cctype>
#include<iomanip>
#include<vector>
#include<cstring>
#include<string>
#include<fstream>
#include<stack>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

 bool runnian(int year){
    if(year%400==0 || (year%4==0 && year%100 != 0)){
        return true;
    }
    else return false;
}
 int year1,mouth1,day1,year2,mouth2,day2;

 int longmouth[12]={31,29,31,30,31,30,31,31,30,31,30,31};

 int shortmouth[12]={31,28,31,30,31,30,31,31,30,31,30,31};

 long days(int year,int mouth,int day){
    long amounts = 0;
    int i,j;
    for(i=0;i<year;i++){
        if(runnian(i))amounts++;
    }
    amounts+=year*365;
    if(runnian(year)){
        for(i=0;i<mouth-1;i++)
            amounts+=longmouth[i];
    }
    else{ 
        for(i=0;i<mouth-1;i++)
            amounts+=shortmouth[i];
    }
    amounts+=day;
    return amounts;
}

 int main(){
    int t;
    long d;
    char str[11];
    cin>>t;
    while(t--){
        cin>>str;
        year1=(str[0]-'0')*1000+(str[1]-'0')*100+(str[2]-'0')*10+(str[3]-'0');
        mouth1=(str[5]-'0')*10+(str[6]-'0');
        day1=(str[8]-'0')*10+(str[9]-'0');
        cin>>str;
        year2=(str[0]-'0')*1000+(str[1]-'0')*100+(str[2]-'0')*10+(str[3]-'0');
        mouth2=(str[5]-'0')*10+(str[6]-'0');
        day2=(str[8]-'0')*10+(str[9]-'0');
        if((d=days(year1,mouth1,day1)-days(year2,mouth2,day2))>0)
            cout<<d<<endl;
        else cout<<-d<<endl;
    }
    return 1;
}


Sicily 1814. 日期计算问题

标签:

原文地址:http://blog.csdn.net/christophe123/article/details/45335791

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!