首页 > 试题广场 >

计算天数

[编程题]计算天数
  • 热度指数:5591 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

输入年月日,计算该填是本年的第几天。例如1990 20 日是1990 年的第263 天,2000 年5 月1 日是2000 年第122 天。


输入描述:
输入第一行为样例数m,接下来m行每行3个整数分别表示年月日。


输出描述:
输出m行分别表示题目所求。
示例1

输入

2
1990 9 20
2000 5 1

输出

263
122

备注:
提示:闰年:能被400 正除,或能被4 整除但不能被100整除。每年1、3、5、7、8、10 、12为大月
import java.util.Scanner;

public class DateTest {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int[][] arr = new int[m][3];
        for (int i = 0; i < m; i++){
            arr[i][0] = sc.nextInt();
            arr[i][1] = sc.nextInt();
            arr[i][2] = sc.nextInt();
        }

        for (int i = 0; i < m; i++){
            int num = date(arr[i][0], arr[i][1], arr[i][2]);
            System.out.println(num);
        }

        sc.close();
    }

    static int date (int y, int m, int d){
        int a1[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int a2[] ={0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int sum = 0;
        if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0){
            for (int i = 1; i < m; i++){
                sum += a2[i];
            }
        }else {
            for (int i = 1; i < m; i++){
                sum += a1[i];
            }
        }
        return sum + d;
    }
}


发表于 2020-04-30 16:17:05 回复(0)
Java
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);
        int n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            int year = scanner.nextInt();
            int month = scanner.nextInt();
            int day = scanner.nextInt();
            LocalDate date = LocalDate.of(year, month, day);
            LocalDate date1 = LocalDate.of(year, 1, 1);
            long days = date1.until(date, ChronoUnit.DAYS);
            System.out.println(days+1);
        }
    }
}


发表于 2020-03-20 16:06:06 回复(0)

问题信息

上传者:小小
难度:
2条回答 4274浏览

热门推荐

通过挑战的用户

查看代码