Day of Week

46 篇文章 0 订阅
46 篇文章 0 订阅
题目描述:

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
提示:

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

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
 
public class Main{
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        Map<String,Integer> monthMap = new HashMap<String, Integer>();
        monthMap.put("January", 1);
        monthMap.put("February", 2);
        monthMap.put("March", 3);
        monthMap.put("April", 4);
        monthMap.put("May", 5);
        monthMap.put("June", 6);
        monthMap.put("July", 7);
        monthMap.put("August", 8);
        monthMap.put("September", 9);
        monthMap.put("October", 10);
        monthMap.put("November", 11);
        monthMap.put("December", 12);
        Map<Integer,String> weekMap = new HashMap<Integer,String>();
        weekMap.put(1, "Monday");
        weekMap.put(2, "Tuesday");
        weekMap.put(3, "Wednesday");
        weekMap.put(4, "Thursday");
        weekMap.put(5, "Friday");
        weekMap.put(6, "Saturday");
        weekMap.put(7, "Sunday");
         
        Scanner scanner = new Scanner(System.in);
         
        while( scanner.hasNext() ){
            String date = scanner.nextLine();
             
            date = formatDate(date,monthMap);
 
            String today = "20140507";
            int resultDayOfWeek = -1;
             
             
            if( cmpDate(today,date) > 0 ){
                int days = getDaysNumBetween(date,today);
                resultDayOfWeek = days % 7 + 3;
                if(resultDayOfWeek > 7) resultDayOfWeek = resultDayOfWeek - 7;
            }else if(cmpDate(today,date) < 0 ){
                int days = getDaysNumBetween(today,date);
                resultDayOfWeek = 3 - days % 7;
                if(resultDayOfWeek <= 0) resultDayOfWeek = resultDayOfWeek + 7;
                 
 
            }else if(cmpDate(today,date) == 0 ){
                resultDayOfWeek = 3;
            }
             
            System.out.println(weekMap.get(resultDayOfWeek));
             
        }
    }
     
     
     
    private static String formatDate(String date, Map<String,Integer> monthMap) {
         
        String temp[] = date.split(" ");
         
        if(temp[0].length() == 1){
            temp[0] = "0" + temp[0];
        }
        temp[1] = monthMap.get(temp[1]).toString();
        if(temp[1].length() == 1){
            temp[1] = "0" + temp[1];
        }
         
        return temp[2]+temp[1]+temp[0];
         
    }
 
 
 
    private static int getDaysNumBetween(String date1, String date2) {
        int year1 = Integer.parseInt( date1.substring(0, 4) );
        int month1 = Integer.parseInt( date1.substring(4, 6) );
        int day1 = Integer.parseInt( date1.substring(6, 8) );
        int year2 = Integer.parseInt( date2.substring(0, 4) );
        int month2 = Integer.parseInt( date2.substring(4, 6) );
        int day2 = Integer.parseInt( date2.substring(6, 8) );
        int count = 0;
        while(   !(year1 == year2 && month1 == month2 && day1 == day2)){
            if( day2 < getDayNum(year2,month2)){
                day2 ++;
                count ++;
            }else{
                if(month2 < 12){
                    month2 ++;
                    day2 = 1;
                }else{
                    year2++;
                    month2 = 1;
                    day2 = 1;
                }
                count ++;
            }
             
             
        }
        return count;
    }
 
 
 
    public static int cmpDate(String date1, String date2){
        int year1 = Integer.parseInt( date1.substring(0, 4) );
        int month1 = Integer.parseInt( date1.substring(4, 6) );
        int day1 = Integer.parseInt( date1.substring(6, 8) );
        int year2 = Integer.parseInt( date2.substring(0, 4) );
        int month2 = Integer.parseInt( date2.substring(4, 6) );
        int day2 = Integer.parseInt( date2.substring(6, 8) );
         
        if(year2 > year1){
            return 1;
        }else if(year2 < year1){
            return -1;
        }else if(month2 > month1){
            return 1;
        }else if(month2 < month1){
            return -1;
        }else if(day2 > day1){
            return 1;
        }else if(day2 < day1){
            return -1;
        }else{
            return 0;
        }
         
    }
     
     
    public static int getDayNum(int year, int month){
        if(month == 1 ||
           month == 3 ||
           month == 5 ||
           month == 7 ||
           month == 8 ||
           month == 10 ||
           month == 12){
            return 31;
        }else if(month == 2){
            if(isLeapYear(year)){
                return 29;
            }else{
                return 28;
            }
        }else{
            return 30;
        }
         
         
    }
 
    private static boolean isLeapYear(int year) {
        return (year%4 == 0 && year%100 != 0) || year%400 == 0;
    }
 
}
 
/**************************************************************
    Problem: 1043
    User: yihukurama
    Language: Java
    Result: Accepted
    Time:110 ms
    Memory:17236 kb
****************************************************************/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值