演算法筆記_154:演算法提高 日期計算(Java)

來源:互聯網
上載者:User

標籤:main   code   alt   count   new   []   資料   代碼   else   

目錄

1 問題描述

2 解決方案

  1 問題描述問題描述  已知2011年11月11日是星期五,問YYYY年MM月DD日是星期幾?注意考慮閏年的情況。尤其是逢百年不閏,逢400年閏的情況。輸入格式  輸入只有一行
  YYYY MM DD輸出格式  輸出只有一行
  W資料規模和約定  1599 <= YYYY <= 2999
  1 <= MM <= 12
  1 <= DD <= 31,且確保測試範例中YYYY年MM月DD日是一個合理日期
  1 <= W <= 7,分別代表周一到周日範例輸入2011 11 11範例輸出5

 

 

2 解決方案

 

具體代碼如下:

import java.util.Scanner;public class Main {        public boolean judgeYear(int year) {        if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))            return true;  //此時為閏年        return false;   //此時為平年    }        public int getMonth(int year, int month) {  //擷取月份天數        if(month == 1 || month == 3 || month == 5 || month == 7 ||                 month == 8 || month == 10 || month == 12)            return 31;        if(month == 4 || month == 6 || month == 9 || month == 11)            return 30;        if(judgeYear(year) && month == 2)            return 29;        if(!judgeYear(year) && month == 2)            return 28;        return 0;    }        public void getResult(String date) {        String[] temp = date.split(" ");        int year = Integer.valueOf(temp[0]);        int month = Integer.valueOf(temp[1]);        int day = Integer.valueOf(temp[2]);        int countDay = 0;        int result = 0;  //最終結果        if(year > 2011) {            for(int i = 1;i < month;i++)                countDay += getMonth(year, i);            countDay += day;            for(int i = year - 1;i > 2011;i--) {  //跳出迴圈時i = 2011                if(judgeYear(i))                    countDay += 366;                else                    countDay += 365;            }            countDay = countDay + 19 + 31;  //加上2011年11月份剩餘天數和12月份31天            result = 5 + countDay % 7;            if(result != 7)                result = result % 7;        } else if(year < 2011){            countDay = countDay + (getMonth(year, month) - day);            month++;            for(;month <= 12;month++)                countDay += getMonth(year, month);            for(int i = year + 1;i < 2011;i++) {  //跳出迴圈時i = 2011                if(judgeYear(i))                    countDay += 366;                else                    countDay += 365;            }            for(int i = 1;i <= 10;i++)                countDay += getMonth(2011, i);            countDay += 11;            result = 5 - countDay % 7;            if(result == 0)                result = 7;            else if(result == -1)                result = 6;            else if(result == -2)                result = 5;        } else if(year == 2011) {            if(month > 11) {                countDay = countDay + 19 + day;            } else if(month == 11) {                if(day >= 11) {                    countDay = day - 11;                } else if(day < 11)                    countDay = 11 - day;            } else if(month < 11) {                countDay += getMonth(2011, month) - day;                int i = month + 1;                for(;i <= 10;i++)                    countDay += getMonth(2011,i);                countDay += 11;            }            if(month >= 11 && day >= 11) {                result = 5 + countDay % 7;                if(result != 7)                    result = result % 7;            } else {                result = 5 - countDay % 7;                if(result == 0)                    result = 7;                else if(result == -1)                    result = 6;                else if(result == -2)                    result = 5;            }        }        System.out.println(result);    }        public static void main(String[] args) {        Main test = new Main();        Scanner in = new Scanner(System.in);        String date = in.nextLine();        test.getResult(date);    }}

 

演算法筆記_154:演算法提高 日期計算(Java)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.