首页 > 试题广场 >

计算一年中的第几天

[编程题]计算一年中的第几天
  • 热度指数:6076 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解

今年的第几天?

输入年、月、日,计算该天是本年的第几天。


输入描述:
包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。


输出描述:
输入可能有多组测试数据,对于每一组测试数据,
输出一个整数,代表Input中的年、月、日对应本年的第几天。
示例1

输入

1990 9 20
2000 5 1

输出

263
122
        int y = in.nextInt();
        int m = in.nextInt();
        int d = in.nextInt();

       Calendar cl =  Calendar.getInstance();
       cl.set(Calendar.YEAR,y);
       cl.set(Calendar.MONTH,m-1);
       cl.set(Calendar.DAY_OF_MONTH,d);

       System.out.println(cl.get(Calendar.DAY_OF_YEAR));
发表于 2025-12-06 20:23:14 回复(0)
import java.util.Scanner;
import java.time.LocalDate;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int year = in.nextInt();
            int month = in.nextInt();
            int day = in.nextInt();
            LocalDate dateTime = LocalDate.of(year,month,day);
            System.out.println(dateTime.getDayOfYear());

        }
    }
}
邪修方法
发表于 2025-10-14 15:52:28 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 每个月的天数,平年
        int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        // 循环处理多组测试数据
        while (scanner.hasNext()) {
            int year = scanner.nextInt();
            int month = scanner.nextInt();
            int day = scanner.nextInt();

            // 判断是否为闰年
            boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

            // 计算总天数
            int totalDays = 0;
            // 累加前面月份的天数
            for (int i = 0; i < month - 1; i++) {
                totalDays += daysInMonth[i];
            }
            // 加上当月的天数
            totalDays += day;
            // 如果是闰年且月份大于2月,加1天
            if (isLeapYear && month > 2) {
                totalDays++;
            }

            System.out.println(totalDays);
        }

        scanner.close();
    }
}

发表于 2025-08-28 18:42:44 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别

        HashMap<Integer, Integer> pingYear = new HashMap<>();
        pingYear.put(1, 31);
        pingYear.put(2, 28);
        pingYear.put(3, 31);
        pingYear.put(4, 30);
        pingYear.put(5, 31);
        pingYear.put(6, 30);
        pingYear.put(7, 31);
        pingYear.put(8, 31);
        pingYear.put(9, 30);
        pingYear.put(10, 31);
        pingYear.put(11, 30);

        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int day  = 0;

            for (Map.Entry<Integer, Integer> map : pingYear.entrySet()) {
                int key = map.getKey();
                int value = map.getValue();
                if ( key > b - 1) {
                    day = day + c;
                    break;
                }
                day = day + value;
            }
            if (isRunYear(a)) {
                if (b > 1) {
                    day += 1;
                }
            }
            System.out.println(day);

        }
    }

    public static boolean isRunYear(int year) {
        return year % 4 == 0 && year % 100 != 0
               ||  year % 400 == 0;
    }
}

发表于 2025-07-20 11:42:18 回复(0)
题目变了 原来只判断一个 现在需要循环判断n个 所以榜上前面的都是旧代码 新的要求的代码达不到那么快
发表于 2025-06-30 14:07:22 回复(0)
import java.util.Scanner;
import java.util.Calendar;
public class Main{
    public static void main(String []args){
        Scanner scan=new Scanner(System.in);
        int year=scan.nextInt();
        int month=scan.nextInt();
        int day=scan.nextInt();
        Calendar cal=Calendar.getInstance();
        cal.set(year,month-1,day);
        System.out.println(cal.get(Calendar.DAY_OF_YEAR));
    }
}

发表于 2018-02-13 11:52:02 回复(0)