计算日期到天数转换

根据输入的日期,计算是这一年的第几天。保证年份为4位数且日期合法。

进阶:时间复杂度: O(n) ,空间复杂度: O(1)

**输入描述

😗*

输入一行,每行空格分割,分别是年,月,日
输出描述:
输出是这一年的第几天

示例1

输入

2012 12 31

输出

366

示例2

输入

1982 3 4

输出

63

Java 编程

package cn.net.javapub.javaintroduction.example;

/**
 * @author: shiyuwang

 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {
            String[] data = str.split(" ");
            int year = Integer.parseInt(data[0]);
            int mouth = Integer.parseInt(data[1]);
            int day = Integer.parseInt(data[2]);

            int[] arr = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

            if (year % 100 == 0 && year % 400 == 0 && mouth > 2) {
                System.out.println(arr[mouth - 1] + day + 1);
            } else if (year % 100 != 0 && year % 4 == 0) {
                System.out.println(arr[mouth - 1] + day + 1);
            } else {
                System.out.println(arr[mouth - 1] + day);
            }


        }
    }
}

展示效果:

华为OD机试 - 计算日期到天数转换 (Java 2024 E卷 100分)_java


华为OD机试 - 计算日期到天数转换 (Java 2024 E卷 100分)_华为od_02