杭电ACM--2005第几天
第几天?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 37271 Accepted Submission(s): 14266
Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
71
1 import java.util.Scanner; 2 public class Main{ 3 public static boolean isLeapYear(int year){ 4 if(year%400==0||(year%4==0&&year%100!=0)) 5 return true; 6 else 7 return false; 8 } 9 public static int getNumberOfDaysInMonth(int year,int month){ 10 int flag = 0; 11 if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) 12 flag = 31; 13 else if(month==2){ 14 flag = isLeapYear(year)?29:28; 15 } 16 else if(month==2||month==4||month==6||month==9||month==11) 17 flag = 30; 18 return flag; 19 } 20 public static void main(String[] args){ 21 Scanner scan = new Scanner(System.in); 22 int year,month,day = 0; 23 int whichday = 0; 24 String str = null; 25 while(scan.hasNext()){ 26 str = scan.nextLine(); 27 String[] date = str.split("/"); 28 year = Integer.parseInt(date[0]); 29 month = Integer.parseInt(date[1]); 30 day = Integer.parseInt(date[2]); 31 whichday = day; 32 for(int i=1;i<month;i++){ 33 whichday += getNumberOfDaysInMonth(year,i); 34 } 35 System.out.println(whichday); 36 } 37 } 38 }