CCF系列之日期计算(201509-2)

本文解析了一道关于计算特定年份中某一天对应月份和日期的问题。通过判断是否为闰年并使用数组记录每个月的天数来实现,最终输出对应的月份和日期。

试题编号: 201509-2
时间限制: 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

 

解题思路:

代码(java):

  

 1 package ccf_text2015_09;
 2 
 3 import java.util.Scanner;
 4 
 5 public class DateCalculate {
 6     
 7     public static void main(String[] args) {
 8         
 9         int monthDay[]={31,28,31,30,31,30,31,31,30,31,30,31};
10         
11         Scanner input = new Scanner(System.in);
12         
13         int year = input.nextInt();
14         
15         input.nextLine();
16         
17         int day = input.nextInt();
18         
19         int count = 0;
20         
21         int month = 0;
22         
23         boolean isLeapYear = ((year%4==0)&&(year%100!=0))||(year%400==0);
24         
25         if(isLeapYear){
26             
27             monthDay[1]=29;
28         }
29         for(int i=0; (i < 12) && (count < day); i++){
30             
31             count+=monthDay[i];    
32             
33             month=i;
34         }
35         count -= monthDay[month];
36         
37         System.out.println(month + 1);
38         
39         System.out.println(day - count);
40          
41     }
42 }

结果:

  

转载于:https://www.cnblogs.com/haimishasha/p/5329877.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值