华为算法精英赛(题1:判断输入天数为当年的第几天)

参加华为算法精英赛,初级赛题要求输入日期(格式:xxxx-xx-xx),计算该日期为当年的第几天。例如,输入2019-1-2,输出应为2。
摘要由CSDN通过智能技术生成

比赛地址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;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值