文章目录
- 1. 求1+2+3+...+n
- 2. 计算是这一年的第几天
- 3. 求两个日期之间的天数
- 4. 算出第n天是几月几号
- 5. 计算一个日期加上若干天后是什么日期
1. 求1+2+3+…+n
求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C
)。
数据范围: 0 < n <= 200
class func{
public:
static int i;
static int sum;
func(){
sum+=i;
++i;
}
};
int func::i = 1;
int func::sum = 0;
class Solution {
public:
int Sum_Solution(int n) {
func* p = new func[n];
return func::sum;
}
};
2. 计算是这一年的第几天
根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
#include <iostream>
using namespace std;
class Date{
public:
int year;
int month;
int day;
Date(){};
// 声明友元
friend istream& operator>>(istream& in, Date& d);
// 判断闰年
bool isLeapYear(int year) const{
return((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
// 获得日期
int Getday(int year, int month) const{
static const int Getdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(month == 2 && isLeapYear(year))
return 29;
return Getdays[month - 1];
}
// 计算是这一年的第几天
int my_sum(int year, int month, int day) const{
int sum = 0;
for(int i = 1;i < month; ++i){
sum += Getday(year, i);
}
sum += day;
return sum;
}
};
// >> 操作符重载
istream& operator>>(istream& in, Date& d){
in >> d.year >> d.month >> d.day;
return in;
}
int main()
{
Date d;
while(cin >> d){
cout << d.my_sum(d.year, d.month, d.day) << endl;
}
return 0;
}
3. 求两个日期之间的天数
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天
#include <bits/stdc++.h>
using namespace std;
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int leap(int year){
if((year%4==0 && year%100!=0) || year%400==0) return 1;
return 0;
}
int main(){
int day1,day2,mon1,mon2,year1,year2;
scanf("%4d%2d%2d",&year1,&mon1,&day1);
scanf("%4d%2d%2d",&year2,&mon2,&day2);
int sum1=0,sum2=0;
for(int yy=0;yy<year1;yy++){
if(leap(yy)) sum1+=366;
else sum1+=365;
}
if(leap(year1)) day[2]=29;
else day[2]=28;
for(int mm=1;mm<mon1;mm++){
sum1+=day[mm];
}
sum1+=day1;
for(int yy=0;yy<year2;yy++){
if(leap(yy)) sum2+=366;
else sum2+=365;
}
if(leap(year2)) day[2]=29;
else day[2]=28;
for(int mm=1;mm<mon2;mm++){
sum2+=day[mm];
}
sum2+=day2;
cout<<abs(sum1-sum2)+1<<endl;
return 0;
}
4. 算出第n天是几月几号
给出年分m和一年中的第n天,算出第n天是几月几号。
#include <iostream>
using namespace std;
class Date {
public:
// 判断是否为闰年
bool isLeapYear(int year) const {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
return false;
}
// 获取某年某月的天数
int GetMonthDay(int year, int month) const {
static const int GetMonthDay[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && isLeapYear(year))
return 29;
return GetMonthDay[month - 1];
}
// 构造函数
Date(int year, int x) {
this->year = year;
if (x <= 31) {
month = 1;
day = x;
} else {
x -= 31;
month = 2;
for (int i = 2; x > GetMonthDay(year, i); ++i) {
++month;
x -= GetMonthDay(year, i);
}
day = x;
}
}
private:
int year;
int month;
int day;
friend ostream& operator<<(ostream& out, const Date& d);
};
// << 运算符重载
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) out << d.year << "-0" << d.month << "-" << d.day <<endl;
else if (d.day < 10) out << d.year << "-" << d.month << "-0" << d.day << endl;
else out << d.year << "-" << d.month << "-" << d.day << endl;
return out;
}
int main() {
int year, x;
while (cin >> year >> x){
Date d(year, x);
cout << d;
}
return 0;
}
5. 计算一个日期加上若干天后是什么日期
设计一个程序能计算一个日期加上若干天后是什么日期。
#include <iostream>
using namespace std;
class Date {
public:
// 判断是否为闰年
bool isLeapYear(int year) const {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
return false;
}
// 获取某年某月的天数
int GetMonthDay(int year, int month) const {
static const int GetMonthDay[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && isLeapYear(year))
return 29;
return GetMonthDay[month - 1];
}
// 日期天数递增一天
void incrementDate() {
++day;
if (day > GetMonthDay(year, month)) {
day = 1;
++month;
if (month > 12) {
month = 1;
++year;
}
}
}
// 后置++运算符重载
Date& operator++(){
Date tmp = *this;
incrementDate();
return *this;
}
Date(int year, int month, int day): year(year), month(month), day(day) {}
private:
int year;
int month;
int day;
friend ostream& operator<<(ostream& out, const Date& d);
};
// << 运算符重载
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) out << d.year << "-0" << d.month << "-" << d.day <<endl;
else if (d.day < 10) out << d.year << "-" << d.month << "-0" << d.day << endl;
else out << d.year << "-" << d.month << "-" << d.day << endl;
return out;
}
int main() {
int n;
int y, m, d, x;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> y >> m >> d >> x;
Date d1(y, m, d);
while(x--){
++d1;
}
cout << d1;
}
return 0;
}