目录
1、求1+2+3+...+n
题目链接:
题目:
题目描述:
解题思路:
代码:
2 计算日期到天数转换
题目链接:
题目:
题目描述:
解题思路:
代码:
3 日期差值
题目链接:
题目:
题目描述:
解题思路:
代码:
4 打印日期
题目链接:
题目:
题目描述:
解题思路:
代码:
5 日期累加
题目链接:
题目:
题目描述:
解题思路:
代码:
1、求1+2+3+...+n
题目链接:
求1+2+3+……+nhttps://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1?tpId=13&tqId=11200&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目:
题目描述:
描述
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
数据范围: 0<�≤2000<n≤200
进阶: 空间复杂度 �(1)O(1) ,时间复杂度 �(�)O(n)示例1
输入:
5返回值:15
示例2
输入:
1返回值:1
解题思路:
运用类的static成员变量的特性进行求解,单独再定义一个类,给定两个静态成员,一个是用来计数的,另一个是用来求和的,随后在该类的构造函数中让计数的静态成员+1,并将计数的静态变量+到求和静态变量中,也就是只要调用构造函数,就会对静态成员变量进行++并求和。再另一个类中定义一个有静态成员类的数组,数组个数为n。随后将最后的求和静态变量返回即可!
代码:
class Sum { public: //构造函数 Sum() { _ret+=_i; ++_i; } static int GetRet() { return _ret; } private: static int _i; static int _ret; }; //定义 int Sum::_i=1; int Sum::_ret=0; class Solution { public: int Sum_Solution(int n) { Sum a[n]; return Sum::GetRet(); } };
2 计算日期到天数转换
题目链接:
HJ73 计算日期到天数转换https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded?tpId=37&&tqId=21296&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking
题目:
题目描述:
描述
根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
进阶:时间复杂度: O(n) ,空间复杂度: O(1)
输入描述:
输入一行,每行空格分割,分别是年,月,日
输出描述:
输出是这一年的第几天
示例1
输入:
2012 12 31输出:
366示例2
输入:
1982 3 4输出:
63
解题思路:
分别定义三个变量,输入年月日,再利用计算月份天数的函数,把该年从一月到我们所输入月之间的每个月的天数累加起来,最后再加上我们输入的天数!就是该年的天数!
代码:
#include <iostream> using namespace std; //计算月份天数函数 int GetMonthDay(int year,int month) { static int Day[13]={0,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; } return Day[month]; } int main() { int year=0; int month=0; int day=0; cin>>year>>month>>day; int sum=0;//求和 int i=1; while(i<month) { //累加天数 sum+=GetMonthDay(year, i); i++; } //再加上我们的输入的天数 sum+=day; cout<<sum; }
3 日期差值
题目链接:
KY111 日期差值https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c?tpId=62&&tqId=29468&rp=1&ru=/activity/oj&qru=/ta/sju-kaoyan/question-ranking
题目:
题目描述:
描述
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天
输入描述:
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
输出描述:
每组数据输出一行,即日期差值
示例1
输入:
20110412 20110422输出:
11
解题思路:
首先输入的是两个整数,需要将整数按照时间日期和天数的格式,分别格式化输入到三个变量中,随后定义一个日期类,分别用输入的日期来构造对象,两个日期之间的天数,就是让小的日期每次+1天,++到跟大的日期相等就是他们之间的天数!
代码:
#include <iostream> using namespace std; class Date { public: //构造函数 Date(int year = 1, int month = 1, int day = 1) :_year(year), _month(month), _day(day) {} //日期天数函数 int GetMonthDay(int year, int month) { static int MonDay[13] = { 0,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; } return MonDay[month]; } //重载+=运算符 Date& operator+=(const Date& d) { _day += d._day; while (_day > GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); ++_month; if (_month == 13) { ++_year; _month = 1; } } return *this; } //重载==运算符 bool operator==(const Date& d) { return _year == d._year && _month == d._month && _day == d._day; } //重载!=运算符 bool operator!=(const Date& d) { return !(*this == d); } //重载++运算符 Date& operator++() { *this += 1; return *this; } //重载>运算符 bool 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; } private: int _year; int _month; int _day; }; int main() { int year1=0; int month1=0; int day1=0; int year2=0; int month2=0; int day2=0; scanf("%4d %2d %2d",&year1,&month1,&day1); scanf("%4d %2d %2d",&year2,&month2,&day2); Date d1(year1,month1,day1); Date d2(year2,month2,day2); Date max=d1; Date min=d2; if(d2>d1) { max=d2; min=d1; } int day=0;//天数 while(min!=max) { ++day; ++min; } cout<<day+1<<endl; // cout<<year1<<" "<<month1<<" "<<day1<<endl; // cout<<year2<<" "<<month2<<" "<<day2<<endl; return 0; }
4 打印日期
题目链接:
KY222 打印日期给出年分m和一年中的第n天,算出第n天是几月几号。。题目来自【牛客题霸】https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b?tpId=69&&tqId=29669&rp=1&ru=/activity/oj&qru=/ta/hust-kaoyan/question-ranking
题目:
题目描述:
描述
给出年分m和一年中的第n天,算出第n天是几月几号。
输入描述:
输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。
输出描述:
可能有多组测试数据,对于每组数据, 按 yyyy-mm-dd的格式将输入中对应的日期打印出来。
示例1
输入:
2000 3 2000 31 2000 40 2000 60 2000 61 2001 60输出:
2000-01-03 2000-01-31 2000-02-09 2000-02-29 2000-03-01 2001-03-01
解题思路:
实现一个日期类,随后用我们输入的年份,只用年份来构造日期对象,让该日期对象加上我们所输入的天数即可。日期类内需要重载+运算符,还需要写一个计算日期函数,就可计算出答案!
代码:
#include <iostream> using namespace std; //日期类 class Date { public: //构造函数 Date(int year = 0, int month = 0, int day = 0) :_year(year), _month(month), _day(day) {} static int GetMonthDay(int year, int month) { static int Day[13] = { 0,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; } return Day[month]; } //重载+运算符 (日期+天数) Date operator+(int day) { Date tmp = *this; tmp._day += day; while (tmp._day > GetMonthDay(tmp._year, tmp._month)) { tmp._day -= GetMonthDay(tmp._year, tmp._month); ++tmp._month; if (tmp._month == 13) { ++tmp._year; tmp._month = 1; } } return tmp; } //打印 void Print() { printf("%d-%02d-%02d\n", _year, _month, _day); } private: int _year; int _month; int _day; }; int main() { int year=0; int day=0; while(cin>>year>>day) { Date d1(year); Date tmp=d1+day; tmp.Print(); } }
5 日期累加
题目链接:
KY258 日期累加设计一个程序能计算一个日期加上若干天后是什么日期。。题目来自【牛客题霸】https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d?tpId=40&&tqId=31013&rp=1&ru=/activity/oj&qru=/ta/kaoyan/question-ranking
题目:
题目描述:
描述
设计一个程序能计算一个日期加上若干天后是什么日期。
输入描述:
输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。
输出描述:
输出m行,每行按yyyy-mm-dd的个数输出。
示例1
输入:
1 2008 2 3 100输出:
2008-05-13
解题思路:
写一个日期类,重载+ 运算符(日期+天数),再写一个计算日期函数即可!
代码:
#include <iostream> using namespace std; //日期类 class Date { public: //构造函数 Date(int year = 0, int month = 0, int day = 0) :_year(year), _month(month), _day(day) {} static int GetMonthDay(int year, int month) { static int Day[13] = { 0,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; } return Day[month]; } //重载+运算符 (日期+天数) Date operator+(int day) { Date tmp = *this; tmp._day += day; while (tmp._day > GetMonthDay(tmp._year, tmp._month)) { tmp._day -= GetMonthDay(tmp._year, tmp._month); ++tmp._month; if (tmp._month == 13) { ++tmp._year; tmp._month = 1; } } return tmp; } //打印 void Print() { printf("%d-%02d-%02d\n", _year, _month, _day); } private: int _year; int _month; int _day; }; int main() { int n=0; cin>>n; int year=0; int month=0; int day=0; int m=0; while(n--) { cin>>year>>month>>day>>m; Date d1(year,month,day); Date tmp=d1+m; tmp.Print(); } }