当前位置:网站首页>华为机试题——HJ73 计算日期到天数转换
华为机试题——HJ73 计算日期到天数转换
2022-04-22 23:09:00 【青云 --小凡】
描述
根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
进阶:时间复杂度:O(n)\O(n) ,空间复杂度:O(1)\O(1)
输入描述:
输入一行,每行空格分割,分别是年,月,日
输出描述:
输出是这一年的第几天
示例1
输入:
2012 12 31
复制输出:
366
复制
示例2
输入:
1982 3 4
复制输出:
63
#include <iostream>
//2012 12 31
int date2day(std::string date)
{
int count = 0;
int len = date.length();
// std::cout << date << std::endl;
int index = date.find(' ');
int year = atoi(date.substr(0, index).c_str());
date = date.substr(index + 1, len - index - 1);
index = date.find(' ');
int month = atoi(date.substr(0, index).c_str());
date = date.substr(index + 1, len - index - 1);
index = date.find(' ');
int day = atoi(date.substr(0, index).c_str());
if((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0))
{
switch (month)
{
case 1:
count = day;
break;
case 2:
count = day + 31;
break;
case 3:
count = day + 60;
break;
case 4:
count = day + 91;
break;
case 5:
count = day + 121;
break;
case 6:
count = day + 152;
break;
case 7:
count = day + 182;
break;
case 8:
count = day + 213;
break;
case 9:
count = day + 244;
break;
case 10:
count = day + 274;
break;
case 11:
count = day + 305;
break;
case 12:
count = day + 335;
break;
default:
break;
}
}
else
{
switch (month)
{
case 1:
count = day;
break;
case 2:
count = day + 31;
break;
case 3:
count = day + 59;
break;
case 4:
count = day + 90;
break;
case 5:
count = day + 120;
break;
case 6:
count = day + 151;
break;
case 7:
count = day + 181;
break;
case 8:
count = day + 212;
break;
case 9:
count = day + 243;
break;
case 10:
count = day + 273;
break;
case 11:
count = day + 304;
break;
case 12:
count = day + 334;
break;
default:
break;
}
}
return count;
}
int main()
{
std::string date;
while(getline(std::cin, date))
{
std::cout << date2day(date) << std::endl;
}
return 0;
}
版权声明
本文为[青云 --小凡]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_41765969/article/details/124355603
边栏推荐
- how to become professional
- Canal usage process, deployment and installation documents
- C add log4net log (console and file) to console application
- [graduation project] defense skills 1 (as a person who came here, I wish you all had a defense)
- How to modify the QR code style when scanning the code and logging in the background management system of enterprise wechat
- 新闻速递 I MobTech通过中国信通院“安全专项评测”
- ASP connect MSSQL database statement
- Excel VBA 多条件筛选及汇部统计
- ansible yum仓库
- Cache and buffer
猜你喜欢

English | day13,14 x sentence true research daily sentence (parallel and nested structure)

《动态规划入门》刷题笔记(更新中)

CVPR 2022: is smile recognition also sexist? Zhejiang University and Wuhan University jointly set up a fairness improvement framework

用 Flutter 写一个精美的登录页面(最新版)

SystemVerilog 验证-测试平台编写指南学习笔记(5):功能覆盖率
![[reproduction of thesis code] errors encountered in the translation embeddings for modeling multi relational data](/img/80/43b7cedaa4c5e98cac61c79dfa37bd.png)
[reproduction of thesis code] errors encountered in the translation embeddings for modeling multi relational data

在线YAML转XML工具

登录功能&新增文章功能的测试点提取以及测试用例编写
![Le Code [SWIFT] déclenche l'événement de clic d'uibutton](/img/23/444cfab44afe32d0476ba26f64bfdf.png)
Le Code [SWIFT] déclenche l'événement de clic d'uibutton

News Express I mobtech passed the "special safety evaluation" of China Academy of information and communications
随机推荐
如何安装cadence 软件
Redis deployment
【板栗糖GIS】编程的本质—(视频笔记)
Excel VBA 多条件筛选及汇部统计
LeetCode 16. 最接近的三数之和(中等、数组)day13
STM32 内存分配解析及变量的存储位置
SystemVerilog 验证-测试平台编写指南学习笔记(0):验证导论
[hctf 2018] admin flash session forgery
LeetCode 414. 第三大的数(简单、数组)day13
C add log4net log (console and file) to console application
新闻速递 I MobTech通过中国信通院“安全专项评测”
SystemVerilog 验证-测试平台编写指南学习笔记(3):连接设计和测试平台
Gentoo系统安装步骤详解
Shell script command results are saved to variables with line breaks
High number | [differential calculus and application of multivariate functions] error prone problems and detailed explanation of Li Lin 880
. net 6 when exiting the constructor, the non nullable property "XXX" must contain a non null value.
The article "fluent" is used to select and upload pictures
必刷2022年江西最新消防设施操作员模拟题库及答案
vtkVertex 顶点
SQL语言详解