目录
1、求1+2+3+...+n要求不能用乘除法、for、while、if、else、switch、case等关键字及判断语句(A?B:C)
1、求1+2+3+...+n要求不能用乘除法、for、while、if、else、switch、case等关键字及判断语句(A?B:C)
OJ链接:求1+2+3+...+n_牛客题霸_牛客网 (nowcoder.com)
#include <bits/types/siginfo_t.h>
#include <regex>
class Sum
{
public:
Sum()
{
_ret += _i;
_i++;
}
static int Return()
{
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::Return();
}
};
借用变长数组,开辟具有n个对象Sum的数组,在每次开辟对象的时候,都会去调用一次Sum对象中的Sum函数,由于_i和_ret均为静态成员变量生命周期是全局的,_ret统计每次叠加的结果,在开辟完n个Sum对象后,调用Return函数,返回最后_ret统计的结果
2、计算日期到天数的转换
OJ链接:计算日期到天数转换_牛客题霸_牛客网 (nowcoder.com)
#include <cstdlib>
#include <iostream>
using namespace std;
int GetDay(int year,int month,int day)
{
int count = 0;//统计天数
if(year % 4 == 0 && year % 100 != 0)//判断闰年
{
int MonthDay[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
for(int i = 1;i < month;i++){//由于数组的特殊,所以i应该小于month而不是month-1
count += MonthDay[i];//先加满月数
}
count += day;//剩余的加上提供的天数即可
}
else
{
int MonthDay[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
for(int i = 1;i < month;i++){
count += MonthDay[i];
}
count += day;
}
return count; //返回最终的天数
}
int main() {
int year,month,day;
while (cin >> year >> month >> day) { // 注意 while 处理多个 case
int num = GetDay(year,month,day);
cout<<num<<endl;
}
return 0;
}
3、日期插值
OJ链接:日期差值_牛客题霸_牛客网 (nowcoder.com)
#include<iostream>
using namespace std;
int daytab[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
bool isLeapYear(int year){
if(year%100!=0&&year%4==0||year%400==0)
return 1;
return 0;
}
char a[9],b[9];
int Get(int year,int month,int day)
{
int n = 0;
//记录年差值
for(int i = 0;i < year;i++)//年可以从0年开始
{
if(isLeapYear(i))//闰年比平年天数多1
{
n+=366;
}
else
{
n+=365;
}
}
//记录月差值
for(int i = 1;i < month;i++)//月不能从0月开始必须从1月开始(因为月份数组的大小是13)
{
n+=daytab[isLeapYear(year)][i];//根据isLeapYear函数的返回值是0还是1确定用闰年数组还是平年数组
}
//记录日差值
n+=day;
return n;
}
int main(){
//n为第一个日期相对于0000 00 00的差值,n1为为第二个的差值
int year,month,day,year1,month1,day1,n,n1;
while(cin>>a>>b)
{
n=n1=0;
sscanf(a,"%4d%2d%2d",&year,&month,&day);//向a数组中格式化读入
sscanf(b,"%4d%2d%2d",&year1,&month1,&day1);//向b数组中格式化读入
int count1 = Get(year, month, day);
int count2 = Get(year1, month1, day1);
cout<<abs(count1-count2)+1<<endl;//差值相减取绝对值+1,取绝对值函数abs
}
return 0;
}
注意事项:涉及sscanf函数和abs函数
4、打印日期
OJ链接:打印日期_牛客题霸_牛客网 (nowcoder.com)
#include <iostream>
#include <cstdio>
using namespace std;
int dayTab[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
bool IsLeapYear(int year){//是否为闰年
return (year%4==0&&year%100!=0 )||(year%400==0);
}
int main(){
int year,month,day;
int number=0;//初始化总天数
while(scanf("%d%d",&year,&number)!=EOF)//输入年和总天数
{
month=0;
int row=IsLeapYear(year);//判断采用闰年数组还是平年数组
while(number>dayTab[row][month])//number大于当前月份天数时证明还有整月可以减
{
number-=dayTab[row][month];//总天数减去完整月份的天数
month++;//减完一个整月的天数后进行下一个月份的判断
}
day=number; //不满整月时,剩余的number就是当月的天数
printf("%04d-%02d-%02d\n",year,month,day);//格式化输出
}
return 0;
}
5、累加天数
OJ链接:日期累加_牛客题霸_牛客网 (nowcoder.com)
#include<iostream>
using namespace std;
bool isLeapYear(int year){ //平均每四年一次闰年,但是400的倍数也是闰年
if(year%4==0&&year%100!=0||year%400==0){
return true;
}return false;
}
int daytab[2][13]={ //日期表,记录每个月有多少天(其实只有二月有变化,闰年29,平年28)
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int main(){
int m,year,month,day,n;
cin>>m;//输入样例个数
while(m--)
{
cin>>year>>month>>day>>n;//输入年、月、日、要加的天数
n+=day;//要加的总天数 = 要加的天数 + 日的天数,便于进行整月++
day=0;//例如2008 2 3 100-->2008 2 0 103(方便计算)
while(n>=daytab[isLeapYear(year)][month])//以月为单位,直到最后不满一个月
{
n-=daytab[isLeapYear(year)][month];
if(month==12)//如果month为12月了,则要进位一年
{
month = 1;//month重新置为1月
year++;//年份+1
}
else
{
month++;//将月份+1
}
}
day = n;//此时已经不满一个月了,剩余的天数n就是当前月份的天数
if(day==0)
{//因为是设置从0开始的,所以比如2008 6 30会输出成2008 7 0,要转换一下
month--;//月份减少
day=daytab[isLeapYear(year)][month];
}
printf("%04d-%02d-%02d\n",year,month,day);
}
return 0;
}
6、日期题的简单模板
bool isLeapYear(int year){ //平均每四年一次闰年,但是400的倍数也是闰年
if(year%4==0&&year%100!=0||year%400==0){
return true;
}return false;
}
int daytab[2][13]={ //日期表,记录每个月有多少天(其实只有二月有变化,闰年29,平年28)
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
~over~