题目1043:Day of Week

题目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]);
			
		}		
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值