首页 > 试题广场 >

今年的第几天?

[编程题]今年的第几天?
  • 热度指数:35146 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入年、月、日,计算该天是本年的第几天。

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


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

输入

1990 9 20
2000 5 1

输出

263
122
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNext()) {
			int year = scanner.nextInt();
			int month = scanner.nextInt();
			int day = scanner.nextInt();
			
			int[] monthDay = {0,31,28,31,30,31,30,31,31,30,31,30,31};
			if (year % 4 == 0 && year % 100 != 0 ||year % 400 == 0) {
				monthDay[2] = 29;
			}
			
			int sum = 0;
			for (int i = 0; i < month; i++) {
				sum += monthDay[i];
			}
			
			sum+=day;
			
			System.out.println(sum);
		}
	}
}

发表于 2024-03-15 14:42:45 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static char[][] key = {{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        int[] year = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int[] leap_year = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        while ((s = br.readLine()) != null) {
            String[] ss = s.split(" ");
            int y = Integer.parseInt(ss[0]);
            int m = Integer.parseInt(ss[1]);
            int d = Integer.parseInt(ss[2]);
            int sum = 0;
            for (int i = 0; i < m; i++) {
                if (i == m - 1)
                    sum += d;
                else {
                    if ((y % 4 == 0) || (y % 100 == 0 && y % 400 == 0))
                        sum += leap_year[i];
                    else
                        sum += year[i];
                }
            }
            System.out.println(sum);

        }

    }

}


发表于 2021-04-16 14:18:58 回复(0)
先设两个数组,将闰年和普通年份的日期初始化。
再思考核心就是闰年判断以及日期相加。
year用于判断日期,month用于判断月份相加的天数,最后加上日期, 
public class Main {
    public static int isYear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            return 1;
        }else {
            return 0;
        }
    }

    public static void main(String[] args) {
        int dayMonth[][]={
            {31,28,31,30,31,30,31,31,30,31,30,31},
            {31,29,31,30,31,30,31,31,30,31,30,31}};
        int year,month,day,yearJudge;

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int dayNumber=0;
            year = scanner.nextInt();
            month = scanner.nextInt();
            day = scanner.nextInt();
            yearJudge = isYear(year);
            for (int i = 0; i < month - 1; i++) {
                dayNumber += dayMonth[yearJudge][i];
            }
            dayNumber += day;
            System.out.println(dayNumber);

        }


    }

}

编辑于 2021-03-07 16:13:45 回复(0)

主要是要知道闰年判断的标准

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt())
        {
            int year=sc.nextInt();
            int month=sc.nextInt();
            int day=sc.nextInt();
            System.out.println(getNumOfDay(year,month,day));
        }
    }

    private static int getNumOfDay(int year,int month,int day)
    {
        int result=0;
        int [][] arr=
        {
            {31,28,31,30,31,30,31,31,30,31,30,31},
            {31,29,31,30,31,30,31,31,30,31,30,31}
        };
        if((year%4==0&&year%100!=0)||year%400==0)//如果是闰年
        {
            for(int i=0;i<=month-2;i++)
            {
                result+=arr[1][i];
            }
            result+=day;
        }
        else
        {
            for(int i=0;i<=month-2;i++)
            {
                result+=arr[0][i];
            }
            result+=day;
        }
        return result ; 
    }
}
发表于 2020-03-25 12:15:06 回复(0)
使用Java8的新的时间API
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int year = scanner.nextInt();
            int mouth = scanner.nextInt();
            int day = scanner.nextInt();
            LocalDate date = LocalDate.of(year, mouth, day);
            LocalDate newYear = LocalDate.of(year, 1, 1);
            System.out.println(newYear.until(date, ChronoUnit.DAYS)+1);
        }
    }
}


发表于 2020-03-14 20:28:15 回复(0)