HDU 2005 第几天?
Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20 71
分析:平年2月28天,闰年2月29天,判断闰平年。
AC源代码(Java语言):
import java.util.Arrays; import java.util.Scanner; public class Main{ // public final static double pi = 3.1415927; public static void main(String[] args) { Scanner sin=new Scanner(System.in); while(sin.hasNext()){ int result = 0; int mouth[] ={31,0,31,30,31,30,31,31,30,31,30,31}; String str = sin.nextLine(); String arr[] = str.split("/"); int y = Integer.parseInt(arr[0]); int m = Integer.parseInt(arr[1]); int d = Integer.parseInt(arr[2]); if(m <= 2){ if(m == 1){ result = d; }else{ result = 31 + d; } }else{ mouth[1] = 28; if(isLeapYear(y)){ mouth[1] ++; } for(int i=0; i<m-1; i++){ result += mouth[i]; } result += d; } System.out.println(result); } } public static boolean isLeapYear(int y){ if(y%400==0||(y%4==0&&y%100!=0)) return true; return false; } }