(JAVA)日期计算 -201509-2

来源:互联网 发布:真正的绝望是什么知乎 编辑:程序博客网 时间:2024/06/07 06:21

问题描述

试题编号: 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



代码:100分答案

  这应该是第二题中最简单的一道了吧

import java.util.Scanner;public class Demo20150902 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int year = sc.nextInt();//年份在1900到2015之间(包含1900和2015)while( year<1900 || year > 2015 ){year = sc.nextInt();}//d在1至365之间int day = sc.nextInt();while( day<1 || day>365  ){day = sc.nextInt();}int[] dArr = {31,28,31,30,31,30,31,31,30,31,30,31};//闰年if( (year%4 ==0 && year%100!=0) || year%400 ==0  ){dArr[1] = 29;}int sum = 0;int mouth = 0;int myday = 0;for(int i =0;i < 12;i++){if(sum < day){sum += dArr[i];}if(sum == day){mouth = i+1;myday = dArr[i];break;}else if(sum > day){mouth = i+1;myday = dArr[i]-(sum-day);break;}}System.out.println(mouth);System.out.println(myday); }}


第二次做答案: 

    把输入那里简化了一下

 import java.util.Scanner;public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);// y,表示年份,年份在1900到2015之间(包含1900和2015)// d,d在1至365之间int year =  sc.nextInt();while(year<1900 || year>2015){year =  sc.nextInt();}int date = sc.nextInt();while(date<1 || date>365){date = sc.nextInt();}// 月份 2月  28   1,3,5,7,8,10,12//                  4,6,9,11int[] arr = new int[12];for(int i=0;i<12;i++){if(i==3 || i==5 || i==8 || i==10){arr[i] = 30;}else{arr[i] = 31;}}// 2月arr[1] = 28;//闰年处理//  1) 年份是4的整数倍,而且不是100的整数倍;//  2) 年份是400的整数倍。if((year%4==0 && year%100!=0) || year%400 ==0){arr[1] = 29;}   int count = 0;int mouth = 0;int day = 0;for(int i=0;i<12;i++){count += arr[i];if(count >= date){mouth = i+1;day = date - (count-arr[i]);System.out.println(mouth);System.out.println(day);break;}} }}