日期类的习题

一.求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)

求1+2+3+...+n_牛客题霸_牛客网

#include <utility>
class Sum
{
public:
Sum()
{
   i+=t;
    t++;
}
   
    static int i;
    static int t;
};
int Sum::i=0;
int Sum::t=1;

class Solution
{
public:

    int Sum_Solution(int n) 
    {
       Sum*p=new Sum[n];
       delete []p ;
       return Sum::i;
    }
    
};

二.日期差值

日期差值_牛客题霸_牛客网/activity/oj&qru=/ta/sju-kaoyan/question-ranking

#include <climits>
#include <iostream>
using namespace std;
class Date 
{
friend ostream& operator<<(ostream& out, const Date& d);
friend Date GetDate(int n);
  private:
  int _year;
  int _month;
  int _day;
  public:
  int GetMonthDay(int year,int month)
  {
       static int GetMonth[13]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
       if(_month==2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
       {
        return 29;
       }
       else 
       {
       return GetMonth[month];
       }
  }
 int operator-(const Date& d) const;
  Date& operator++();
  	bool operator<(const Date& d) const;
    bool operator==(const Date& d) const;
	bool operator!=(const Date& d) const;
    Date& operator+=(int day);
};
bool Date::operator<(const Date& d) const
 {
        if (_year < d._year)
            return true;
        else if (_year == d._year && _month < d._month)
            return true;
        else if (_year == d._year && _month == d._month && _day < d._day)
            return true;
        return false;
}

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Date& Date::operator++()
{
	*this+=1;
	return *this;
}
bool Date::operator==(const Date& d) const
{
	return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator!=(const Date& d) const
{
	return !(*this==d);
}

int Date::operator-(const Date& d) const
{
	int n = 0;

	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		min = *this;
		max = d;
	
	}
	while (min != max)
	{
		++min;
		n++;
	}
	return n;
}
Date GetDate(int n)
{
    Date a;
    a._year=n/10000;
    a._month=(n - a._year * 10000) / 100;
    a._day=n%100;
    return a;
}

int main()
{
    int n1,n2;
    Date d1;
    Date d2;
    cin>>n1>>n2;
    d1=GetDate(n1);
    d2=GetDate(n2);
    cout<<d1-d2+1<<endl;
}

三.计算一年的第几天

计算日期到天数转换_牛客题霸_牛客网

#include <ctime>
#include <iostream>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
 private:
    int _year;
    int _day;
    int _month;
};
	int GetMonthDay(int year, int month)
	{
		static int monthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			return 29;
		}
		else
		{
			return monthDay[month];
		}
	}
ostream& operator<<(ostream& out, const Date& d)
{
    int n=0;
    for(int i= d._month;i>1;i--)
    {
     n+=GetMonthDay(d._year,i);
    }
    n+=d._day;
	out<< n <<endl;
	return out;
}
istream& operator>>(istream& in, Date& d)
{	
	in >>  d._year >>  d._month >> d._day;
	return in;
}
int main()
{
    Date a;
    cin>>a;
    cout<<a;
}

四.累加天数

日期累加_牛客题霸_牛客网

#include <climits>
#include <iostream>
using namespace std;
class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
    friend Date Getmonth(int year,int month,int day);
private:
	int _year;
	int _month;
	int _day;
public:
	int GetMonthDay(int year, int month)
	{
		static int GetMonth[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (_month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			return 29;
		}
		else
		{
			return GetMonth[month];
		}
	}
    Date& operator+=(int day);
    Date operator+(int day);


};

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator+(int day)
{
    Date tmp=*this;
    tmp+=day;
    return tmp;

}
ostream& operator<<(ostream& out,  const Date& d)
{
    if(d._month<10 && d._day<10)
    {
        out<<d._year<<"-"<<0<<d._month<<"-"<<0<<d._day;
    }
    else if (d._month<10 && d._day>10)
    {
        out<<d._year<<"-"<<0<<d._month<<"-"<<d._day;
    }
    else if(d._month>=10 && d._day<10)
    {
        out<<d._year<<"-"<<d._month<<"-"<<0<<d._day;
    }
    else
    out<<d._year<<"-"<<d._month<<"-"<<d._day;
    return out;
}


Date Getmonth(int year,int month,int day)
{
  Date b;
  b._year=year;
  b._month=month;
  b._day=day;
  return b;
}


int main()
{
	int m,year,day,month,t;
    cin>>m;
    while(m--)
    {
        cin>>year>>month>>day>>t;
        Date a;
        a=Getmonth(year,month,day);
        cout<<a+t<<endl;

    }

}

五.打印日期

打印日期_牛客题霸_牛客网

#include <iostream>
using namespace std;
class Date 
{   
 friend ostream& operator<<(ostream& out, const Date& d);
 friend  Date Getday(int year,int num);
  private:
  int _year;
  int _month;
  int _day;
  public:  
};
 	int GetMonthDay(int year, int month)
	{
	    static int GetMonth[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			return 29;
		}
		else
		{
			return GetMonth[month];
		}
	} 
  ostream& operator<<(ostream& out, const Date& d)
  {
    if(d._month<10 && d._day<10)
    out<<d._year<<"-"<<0<<d._month<<"-"<<0<<d._day<<endl;
    else if(d._month<10 && d._day>=10)
    {
         out<<d._year<<"-"<<0<<d._month<<"-"<<d._day<<endl;
    }
    else if(d._month>10 && d._day<10)
    {
         out<<d._year<<"-"<<d._month<<"-"<<0<<d._day<<endl;
    }
    else
    {
        out<<d._year<<"-"<<d._month<<"-"<<d._day<<endl;
    }
    return out;
  }
    Date Getday(int year,int num)
    {
        int month=1;
        for(int i=1;i<=12;i++)
        {
            if(num<=GetMonthDay(year,i))
            {
                break;
            }
            else 
            {
            
               num-=GetMonthDay(year,i);
               month++;
               if(month==13)
               {
                 year++;
                 month=1;
               }
              
            
            }
        }
    Date a;
    a._year=year;
    a._month=month;
    a._day=num;
    return a;
    }
int main()
{
    int year,num;
    while((cin>>year>>num))
    {
    Date d;
    d=Getday(year,num);
  cout<<d;
    }
}
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LaNzikinh篮子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值