题目1043:Day of Week

时间:2021-09-19 08:15:20

题目1043:Day of Week

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3505

解决:1300

题目描述:

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.

样例输入:
9 October 2001
14 October 2001
样例输出:
Tuesday
Sunday
参考代码:

import java.util.Scanner;

/*:w = (d + [2.6m - 0.2] + 5(y % 4) + 3y + 5(c %  4)) % 7
d = 日期
m = 月数 - 2(1月为11月,2月为12月)
y = 年数后2位(1、2月份y - 1)
c = 世纪数
 * */

public class Main {
	public static void main(String arg[]){

		//月
		String[] month = {"January", "February", "March", "April", "May", "June"
				 ,"July", "August","September", "October", "November", "December"};
		String[] week = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
		
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			String temp = sc.nextLine();
			String[] str = temp.split(" ");
			Integer day = Integer.parseInt(str[0]);
			//月份计算
			Integer mon=1;
			for(String i:month){
				if(i.equals(str[1])) break;
				mon++;
			}
			Integer m =0;
			if(mon==1 || mon==2) m = mon+10;
			else  m = mon-2;
			
			//年份计算	
			Integer cen = Integer.parseInt(str[2].substring(0, 2));
			Integer year = Integer.parseInt(str[2].substring(2, 4));
			
			Integer y=0;
			if(mon==1 || mon==2) y=year-1;
			else y=year;
			if(y==-1) {
				y=99;
				cen--;
			}
			
			
			Integer w =  (day+(int)(Math.floor(2.6*(float)m-0.2))+5*(y%4)+3*y+5*(cen%4))%7;
			System.out.println(week[w]);
			
		}		
	}

}