CCF-201509-2-日期计算

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AivenZhong/article/details/82056576

时间限制: 1.0s

内存限制: 256.0MB

问题描述:
  给定一个年份y和一个整数d,问这一年的第d天是几月几日?
  注意闰年的2月有29天。满足下面条件之一的是闰年:
  1) 年份是4的整数倍,而且不是100的整数倍;
  2) 年份是400的整数倍。

输入格式
  输入的第一行包含一个整数y,表示年份,年份在1900到2015之间(包含1900和2015)。
  输入的第二行包含一个整数d,d在1至365之间。

输出格式
  输出两行,每行一个整数,分别表示答案的月份和日期。

样例输入
2015
80

样例输出
3
21

样例输入
2000
40

样例输出
2
9

import java.util.Scanner;
public class 日期计算 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int years = input.nextInt();
        int days = input.nextInt();

        int[] months = {31, 28, 31, 30, 31, 30,
                31, 31, 30, 31, 30, 31};

        if(years%400==0||years%4==0&&years%100!=0)
            months[1]++;

        int sum = 0, month;
        for(month=0;month<months.length &&
                sum + months[month]<days;month++)
            sum += months[month];
        System.out.println((month+1)+"\n"+(days-sum));
    }

}

猜你喜欢

转载自blog.csdn.net/AivenZhong/article/details/82056576
今日推荐