华为算法精英赛(题1:判断输入天数为当年的第几天)
比赛地址:https://bbs.huaweicloud.com/forum/thread-29407-1-1.html
>初级赛题:判断输入天数为当年的第几天
·题目介绍
输入一个日期,格式为xxxx-xx-xx,判断这一天为当年的第几天?
·示例
输入:2019-1-2
输出:2
/** * Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements. See the NOTICE file distributed with* this work for additional information regarding copyright ownership.* The ASF licenses this file to You under the Apache License, Version 2.0* (the "License"); you may not use this file except in compliance with* the License. You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.**/
package com.huawei.algorithm;/**
* @desc: algorithm_elite
* @name: JudgeDays.java
* @author: tompai
* @email:liinux@qq.com
* @createTime: 2019年11月29日 下午12:50:18
* @history:
* @version: v1.0
*/import java.util.Scanner;public class JudgeDays {/*** @author: tompai* @createTime: 2019年11月29日 下午12:50:19* @history:* @param args void*/public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入日期,格式:2019-12-1");String date = sc.nextLine();TimeCalculator time = new TimeCalculator(date);System.out.println(time);}
}class TimeCalculator {String date;String[] dateArray;// 年份int year;// 月份int month;// 日期int day;// 总天数int allDay = 365;public TimeCalculator(String date) {this.date = date;this.dateArray = date.trim().split("-");this.year = Integer.parseInt(dateArray[0]);this.month = Integer.parseInt(dateArray[1]);this.day = Integer.parseInt(dateArray[2]);}// 输入指定日期,返回已经过去了多少天public int elapsedTime() {// 计算经历过的天数int elapsedTime = day;for (int i = 1; i < this.month; i++) {elapsedTime = elapsedTime + month(this.year, i);}return elapsedTime;}// 输入指定日期,返回还剩多少天public int daysLeft() {// 计算剩下的天数int daysLeft = allDay - elapsedTime();return daysLeft;}// 判断输入的日期,是否符合格式要求public boolean format() {boolean judge = true;if ((date.length() != 10) && (date.charAt(4) != '-') && (date.charAt(7) != '-')) {System.out.println("您输入的日期有误!!!");judge = false;}return judge;}// 判断是闰年还是平年public boolean isLeapYear(int year) {boolean judge = false;if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {allDay = 366;judge = true;}return judge;}// 输入月份返回共有多少天public int month(int year, int month) {int day = 0;switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:day = 31;break;case 4:case 6:case 9:case 11:day = 30;break;case 2:if (isLeapYear(year)) {day = 29;} else {day = 28;}break;}return day;}// 计算在输入的月份里还剩多少天public int day(int year, int month, int day) {int daysLeft = month(year, month) - day;return daysLeft;}public String toString() {String time = String.valueOf(elapsedTime());;return time;}
}
华为算法精英赛(题1:判断输入天数为当年的第几天)相关推荐
- java判断扑克牌是否为顺子_程序算法设计题,判断扑克牌中的顺子
相信很多人都玩过扑克牌.在扑克牌中,有许许多多的算法供我们学习.仅仅一个斗地主游戏,就可以学习很多的算法.今天和大家分享一个判断扑克牌中的顺子的问题. 题目: 从扑克牌中随机抽取五张牌,判断是不是一个 ...
- 【码蹄集新手村 600 题】判断输入的俩个正整数是否为蛮生质数(蛮生素数)
题目链接: 码蹄集 (matiji.net) 解题思路: 用C语言查找100 ~ 200之间的素数_Sandm *的博客-CSDN博客_c语言素数判断100到200 参考代码: #include< ...
- Java算法:华为机试算法(下),华为算法Java版,牛客网华为算法73~108题
接上篇:Java算法:华为机试算法(中),华为算法Java版,牛客网华为算法55~72题 HJ73 计算日期到天数转换 计算日期到天数转换 题目描述 根据输入的日期,计算是这一年的第几天.. 测试 ...
- 判断输入的字符串是否为回文_刷题之路(九)--判断数字是否回文
Palindrome Number 问题简介:判断输入数字是否是回文,不是返回0,负数返回0 举例: 1: 输入: 121 输出: true 2: 输入: -121 输出: false 解释: 回文为 ...
- Java黑皮书课后题第4章:*4.21(检查SSN)编写一个程序,提示用户输入一个社保号码(格式是DDD-DD-DDDD,D是1个数字)你的程序应该判断输入是否合法
*4.21(检查SSN)编写一个程序,提示用户输入一个社保号码(格式是DDD-DD-DDDD,D是1个数字)你的程序应该判断输入是否合法 题目 题目概述 运行示例 破题 代码 题目 题目概述 *4.2 ...
- 判断二月天数的c语言程序,C语言 输入年月日判断是第几天
判断输入年份为闰年还是平年.主要用于判断二月份的天数. int isLeapYear(int year); int isLeapYear(int year) { int february = 0; i ...
- 用python输入圆的半径、输出圆的周长_Python基础题练习(输入圆的半径计算周长面积,判断年份是否是闰年)...
题目二:输入圆的半径计算周长面积 题目要求:输入圆的半径,输出圆的周长和面积.输入输出均为整数或者浮点数 题目分析: 首先使用关键字import导入math数学函数库 获取到用户输入的参数(圆的半径) ...
- JAVA经典算法40题
JAVA经典算法40题 [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析 ...
- java求公式例题_JAVA经典算法40题
1: JAVA经典算法40题 2: [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 3 ...
- 面试题库 之 数据结构与算法 100题
1.把二元查找树转变成排序的双向链表 题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表. 要求不能创建任何新的结点,只调整指针的指向. 10 / \ 6 14 / \ / \ 4 8 ...
最新文章
- 【Flask】ORM一对一关联关系
- 跨链资产原子转移工具包 Decred atomicswap
- 别再说你不会ElasticSearch,都给你整理好了
- 内向的人在面试时如何表现自己?
- antdesign 柱状图_ant design pro (十)advanced 图表
- 六、 抽象类与接口对比
- ajax存储表单数据,使用ajax json将表单数据存储到数据库php
- 软件项目管理案例教程 第4版 习题答案(测试题)
- 方差公式Var(x)=E(x^2)-[E(x)]^2,求期望值
- java实现根据身份证计算年龄的两种方式
- QQ推广,QQ在线代码
- 如何在SuperMap iDesktop制作卫星地图
- h5 ios Safair下载文件自动添加.html导致文件乱码问题,ios不能使用接口播放视频的问题
- md格式 linux,Linux命令基本格式以及文件处理命令.md
- 服务器 台式机性能比较,服务器与台式机的区别
- rust guessing game
- 近期技术讨论贴(持续更新:12-10)
- win10 vs2017 community 新版 systemc
- Linux进程间通信(五)——进程间通信
- 互联网如何颠覆这些我们熟知的17个行业!