当前位置:网站首页>Huawei machine test question - hj73 calculation date to day conversion
Huawei machine test question - hj73 calculation date to day conversion
2022-04-22 23:13:00 【Qingyun Xiaofan】
describe
According to the entered date , The calculation is the day of the year .
The guarantee year is 4 The number of digits and the date are legal .
Advanced : Time complexity :O(n)\O(n) , Spatial complexity :O(1)\O(1)
Input description :
The input line , Each line is separated by spaces , They're years , month , Japan
Output description :
The output is the day of the year
Example 1
Input :
2012 12 31
Copy output :
366
Copy
Example 2
Input :
1982 3 4
Copy output :
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;
}
版权声明
本文为[Qingyun Xiaofan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222309181557.html
边栏推荐
- 不谈赛道,不聊风口,开源数据库巨头Cassandra如何在国内讲好“新故事”
- [leetcode refers to the path with a certain value in offer 34. Binary tree (medium)]
- Read software engineering at Google (12)
- [reproduction of thesis code] errors encountered in the translation embeddings for modeling multi relational data
- Lire "Software Engineering at Google" (11)
- Memory introduction
- [chestnut sugar GIS] the essence of programming - (video notes)
- 解决require is not defined的报错问题
- 安全产品设计的重要性
- [BJDCTF2020]Easy MD5
猜你喜欢
随机推荐
Gbase 8A common parallelism parameter adjustment to improve performance
STM32 内存分配解析及变量的存储位置
SystemVerilog 验证-测试平台编写指南学习笔记(1):数据类型
必刷2022年江西最新消防设施操作员模拟题库及答案
华为机试题——HJ76 尼科彻斯定理
【板栗糖GIS】编程的本质—(视频笔记)
[HCTF 2018]admin之unicode欺骗
Read software engineering at Google (12)
高数 | 【多元函数微分学及应用】易错题 及 李林880详解
Yolov1 paper notes
Login function & test point extraction of new article function and test case writing
Memory introduction
cache与buffer
Robot OS系统架构设计
Read software engineering at Google (11)
SQL Net message from client 事件产生的原因分析
SystemVerilog 验证-测试平台编写指南学习笔记(2):过程语句和子程序
Shell script command results are saved to variables with line breaks
codeforce round#784(div4) A-H
数值类型和数列类型


![[BJDCTF2020]Easy MD5](/img/94/08b19ead7c48549340b29bc8f34033.png)





