一.求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
#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;
}
}