星期幾

題目:

已知 2012 年是 1 月 25 日是星期三, 編寫(xiě)一個(gè)程序, 輸入用 “年 月 日” 表示的一個(gè)2012年1月25日以后的日期, 輸出該日期是星期幾(星期天輸出 0)。

輸入樣例:

2015 11 02

輸出樣例:

1

思路:2012年1月22日是星期天。算出給定日期是從該天起過(guò)了 x 天, 然后輸出 x%7。

c++實(shí)現(xiàn)

#include <iostream>
#include <cstdio>
using namespace std;
int month_days[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main(void){
    int year, month, date;
    int days = 0;   // 從 2012-01-22開(kāi)始過(guò)了多少天
    cin >> year >> month >> date;
    int y;
    for (y=2012; y<year; y++){
        if (y%4 == 0 && y%100 != 0 || y%400 == 0){
            days += 366;
        } else {
            days += 365;
        }
    }
    if (year%4 == 0 && year%100 != 0 || y%400 == 0){
        month_days[2] = 29;
    }
    int m;
    for (m=1; m<month; m++){
        days += month_days[m];
    }
    days += date;
    days -= 22; // 2012年1月22日是星期天, 日期是從 1 月 1 日開(kāi)始的
    cout << days << endl;
    cout << days%7 << endl;

    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容