OJ题 KY258-日期累加&&KY222-打印日期

在这里插入图片描述
日期累加

明确解题思想:
日期的变化要考虑到:1)这个年是平年还是闰年(平年2月28天,闰年2月29天)
2)每一个月的天数是28、29、30还是31
代码思路阐述:
1:把给定的当前月天数给到累加天数里,循环去减调每个月天数,月++,过了一年则年++
2、每一个月多少天直接以数组形式给出,因为数量不是特别大
3、考虑到如果最后正好卡到了某个月的0号,那要返回上一个月的最后一天

作者:Ansharly
链接:https://www.nowcoder.com/discuss/376063683691126784
来源:牛客网

//判断当前年份是平年还是闰年
bool isleapyear(int year)
{
    if(year%4==0&&year%100!=0 || year%400==0)
        return true;
    else return false;
}
//每个月有几天的数组
//isleapyear返回true即是第1行,返回false即是第0行
//把第一列设置为0的原因是数组下标从0开始,如果是传进来的月份那么这个月份还需要+1;直接把第一列给为0就没问题了
int dayofeachmonth[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}};
 
int main()
{
    int m;
    int year,month,day,n;
    cin>>m;
        //m表示会输出的行数
    while(m--)
    {
        cin>>year>>month>>day>>n;
        n+=day;
        day = 0;
        while(n>=dayofeachmonth[isleapyear(year)][month])
        {
            n-=dayofeachmonth[isleapyear(year)][month];
        if(month == 12)
        {
            month = 1;year++;
        }
        else
        {
            month++;
        }
        }
        day+=n;
                //避免得到的日期是某个月的第0天,这种情况就返回上一个月的最后一天就可以
        if(day == 0)
        {
            month--;
            day=dayofeachmonth[isleapyear(year)][month];
        }
        //cout<<year<<"-"<<month<<"-"<<day<<endl;
        printf("%04d-%02d-%02d\n", year,month, day);
    }
    return 0;
}

思路清晰了你也来试试吧~

KY222-打印日期
在这里插入图片描述
打印日期
思路:和上面这道题目差不多,1、明确年份是否是闰年以及每个月的天数
2、month初始化为0,每将天数减一个月,month++,最后将不足一个月的天数赋值给day输出即可

#include <iostream>
using namespace std;


bool isleapyear(int year)
{
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
        return true;
    else return false;
}
int days[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} };

int main()
 {
    int year, n;
    int day = 0;
     while (cin >> year >> n)
    {
             int month = 0;
             while (n > days[isleapyear(year)][month])
             {
                 n -= days[isleapyear(year)][month];
                 if (month == 12)
                 {
                     month = 1; year++;
                 }
                 month++;
             }
             day = n;//不可以写成day+=n这样会导致下一次循环时都会在这个基础上再进行++,因为未脱离函数,资源未被释放
             printf("%04d-%02d-%02d\n", year, month, day);
    }
    return 0;
}

思路清晰了你也快去试一试吧~

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这是一道经典的位运算目,考察对二进制的理解和位运算的熟练程度。 目描述: 给定一个长度为 $n$ 的数组 $a$,初始时每个数的值都为 $0$。现在有 $m$ 个操作,每个操作为一次询问或修改。 对于询问,给出两个整数 $l,r$,求 $a_l \oplus a_{l+1} \oplus \cdots \oplus a_r$ 的值。 对于修改,给出一个整数 $x$,表示将 $a_x$ 的值加 $1$。 输入格式: 第一行两个整数 $n,m$。 接下来 $m$ 行,每行描述一次操作,格式如下: 1 l r:表示询问区间 $[l,r]$ 的异或和。 2 x:表示将 $a_x$ 的值加 $1$。 输出格式: 对于每个询问操作,输出一个整数表示答案,每个答案占一行。 数据范围: $1 \leq n,m \leq 10^5$,$0 \leq a_i \leq 2^{30}$,$1 \leq l \leq r \leq n$,$1 \leq x \leq n$ 输入样例: 5 5 2 1 2 3 1 2 4 2 2 1 1 5 输出样例: 0 2 解思路: 对于询问操作,可以利用异或的性质,即 $a \oplus b \oplus a = b$,将 $a_l \oplus a_{l+1} \oplus \cdots \oplus a_r$ 转化为 $(a_1 \oplus \cdots \oplus a_{l-1}) \oplus (a_1 \oplus \cdots \oplus a_r)$,因为两个前缀异或后的结果可以相互抵消,最后的结果即为 $a_1 \oplus \cdots \oplus a_{l-1} \oplus a_1 \oplus \cdots \oplus a_r = a_l \oplus \cdots \oplus a_r$。 对于修改操作,可以将 $a_x$ 对应的二进制数的每一位都分离出来,然后对应位置进行修改即可。由于只有加 $1$ 操作,所以只需将最后一位加 $1$ 即可,其余位不变。 参考代码:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值