上海交通大学 Day of Week(java)

题目描述
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400. For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap. Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
输入描述:
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
输出描述:
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
示例1
输入
复制
9 October 2001
14 October 2001

输出
复制
Tuesday
Sunday
import java.util.*;
import java.io.*;
import java.text.* ;
public class Main
{
	public static HashMap<String, Integer> mon = new HashMap<>();
    public static void main(String[] args) throws ParseException{
    	try {
//    		0,31,59,90,120,151,181,212,243,273,304,334
//    		January, February, March, April, May, June, July, August, September, October, November, December
    		mon.put("January", 0);
    		mon.put("February", 31);
    		mon.put("March", 59);
    		mon.put("April", 90);
    		mon.put("May", 120);
    		mon.put("June", 151);
    		mon.put("July", 181);
    		mon.put("August", 212);
    		mon.put("September", 243);
    		mon.put("October", 273);
    		mon.put("November", 304);
    		mon.put("December", 334);
    		String[] date = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
	        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
	        String str;
	        while((str = br.readLine()) != null) {
	        	String[] parts = str.split(" ");
	        	System.out.println(date[getDay(Integer.parseInt(parts[2]), parts[1], Integer.parseInt(parts[0]))%7]);
	        }
 	    } catch (IOException e) {
	        e.printStackTrace();
	    }
    }
    public static int getDay(int year, String month, int day){
        int sum = 0;
        for (int i = 1; i < year; i++) {
            if ((i % 4 == 0 && i % 100 != 0 ) || i % 400 == 0) {
                sum += 366;
            } else {
                sum += 365;
            }
        }
        sum += mon.get(month);
        if (((year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0)&&mon.get(month)>31) sum++;
        sum += day;
        return sum;
    }
}

发布了231 篇原创文章 · 获赞 22 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43306331/article/details/104190342