Dowemo

Question number: 201509 -2

Question name: date calculation

Time limit: 1.0 s

Memory limit: 256.0 mb

Problem description:
Given a year y and an integer d, ask the year for the year to be a few months.
Note 2 months for leap leap months. To meet one of the following conditio & is a leap year:
1 ) the year is a single number of 4, and isn't an integer of 100;
2 ) the year is an integer of 400.

Enter format
The fi & t line entered contai & an integer y that represents the year, between 1900 to 2015 ( with 1900 and 2015 ).
The second line entered contains an integer d, d between 1 to 365.

Output format
Output two lines, one per line, representing the month and date of the answer.

Sample input
2015
80

Sample output
3
21

Sample input
2000
40

Sample output
2
9


import java.util.Scanner;



public class T201509_2 {


 static int y;


 static int d;


 static int m = 1; //从1月开始



 public static void main(String[] args) {


 Scanner sc = new Scanner(System.in);


 y = sc.nextInt();


 d = sc.nextInt();


 sc.close();



 boolean rn = false; //闰年


 if((y%4==0 && y%100!=0) || y%400==0) //是否闰年


 rn = true;



 while(d> 0){



 switch (m) {



 //31天的月份


 case 1:case 3:case 5:case 7:case 8:case 10:case 12:


 cal(31);


 break;



 //30天的月份


 case 4:case 6:case 9:case 11:


 cal(30);


 break;



 //2月


 case 2:


 if(rn) cal(29);


 else cal(28);


 break;



 }



 }


 }



 /**


 * 计算


 * @param day 每一月的天数


 */


 static void cal(int day){


 if(d> day){ //剩余d超过一个月


 d -= day;


 m++; //月份计数


 }else{ //剩余d就在本月


 System.out.println(m); //月


 System.out.println(d); //日


 d = 0; //退出主函数中的while循环


 }


 }


}






这里写图片描述




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs