码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 牛客网KY222打印日期 牛客网KY258累加天数


    题一:
    打印日期_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b?tpId=69&&tqId=29669&rp=1&ru=/activity/oj&qru=/ta/hust-kaoyan/question-ranking

    在完成此题之前需要先掌握一个C语言小知识点:

    C++中%d 和 %04d有什么区别?
    %d左对齐,输出变量的所有数字;%4d右对齐,宽度为4,左边填充空格,当变量的实际宽度大于4时,输出变量的所有数字;%04d与%4d的唯一区别就是左边填充0。例如
    1)以%d,%4d,%04d,输出12时, 结果是:12,两个空格12, 0012。
    2)以%d,%4d,%04d,输出123时, 结果是: 123,一个空格123,0123。
    3)以%d,%4d,%04d,输出1234时,结果是: 1234,1234,1234。
    4)以%d,%4d,%04d,输出12345时,结果是: 12345,12345,12345。

    解析代码:

    1. #include
    2. using namespace std;
    3. int GetMonthDay(int year, int month)
    4. {
    5. static int arr[13] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
    6. int day = arr[month];
    7. if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
    8. {
    9. day += 1;
    10. }
    11. return day;
    12. }
    13. void PrintMonthDay(int year,int dayCount)
    14. {
    15. int month = 1;
    16. while (dayCount > GetMonthDay(year, month))
    17. {
    18. dayCount -= GetMonthDay(year, month);
    19. month++;
    20. if (month == 13)
    21. {
    22. year++;
    23. month = 1;
    24. }
    25. }
    26. printf("%04d-%02d-%02d\n", year, month, dayCount);
    27. }
    28. int main()
    29. {
    30. int year, dayCount;
    31. while (cin >> year >> dayCount)
    32. {
    33. PrintMonthDay(year, dayCount);
    34. }
    35. return 0;
    36. }

    题二:

    日期累加_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d?tpId=40&&tqId=31013&rp=1&ru=/activity/oj&qru=/ta/kaoyan/question-ranking 解析代码:

    1. #include
    2. using namespace std;
    3. class Date
    4. {
    5. public:
    6. Date(int year,int month,int day)
    7. :_year(year)
    8. ,_month(month)
    9. ,_day(day)
    10. {}
    11. int GetMonthDay(int year, int month)
    12. {
    13. static int arr[13] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
    14. int day = arr[month];
    15. if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
    16. {
    17. day += 1;
    18. }
    19. return day;
    20. }
    21. Date& operator+=(int day)
    22. {
    23. _day += day;
    24. while (_day > GetMonthDay(_year, _month))
    25. {
    26. _day -= GetMonthDay(_year, _month);
    27. _month++;
    28. if (_month == 13)
    29. {
    30. _year++;
    31. _month = 1;
    32. }
    33. }
    34. return *this;
    35. }
    36. void Print()
    37. {
    38. printf("%04d-%02d-%02d\n", _year, _month, _day);
    39. }
    40. private:
    41. int _year;
    42. int _month;
    43. int _day;
    44. };
    45. int main()
    46. {
    47. int n = 0;
    48. int year, month, day;
    49. int days;
    50. cin >> n;
    51. while (n)
    52. {
    53. cin >> year >> month >> day;
    54. Date d1(year, month, day);
    55. cin >> days;
    56. d1 += days;
    57. d1.Print();
    58. n--;
    59. }
    60. return 0;
    61. }

  • 相关阅读:
    AI医疗高精尖!基于AI的新药研发!
    基于springboot的计算机类考研交流平台(源码+论文)
    用 Gaussian Process 建模 state-action 空间相关性,加速 Multi-Fidelity RL
    Linux 内核加速支持 Rust 开发;中科院计划每半年升级一次 RISC-V 芯片;Python 3.10.1 发布 | 开源日报
    Web APIs——焦点事件以及小米搜索框
    保护数据隐私:深入探索Golang中的SM4加密解密算法
    C#,数值计算——插值和外推,分段线性插值(Linear_interp)的计算方法与源程序
    【疑难攻关】——XXE漏洞快速入门
    IIC协议
    探索神经网络架构教程视频,设计神经网络的步骤
  • 原文地址:https://blog.csdn.net/weixin_56054625/article/details/126277617
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号