1929 Problem B Day of Week

389 篇文章 1 订阅

问题 B: Day of Week
时间限制: 1 Sec 内存限制: 32 MB
献花: 164 解决: 64
[献花][花圈][TK题库]
题目描述
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.
样例输入
21 December 2012
5 January 2013
样例输出
Friday
Saturday

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool isLeap(int y)
{
    return (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0));
}

int MstringToI(string m)
{
    string M[] = { "", "January","February","March","April","May","June","July",\
        "August","September","October","November","December" };
    int ind = 1;
    for (; ind < 13; ++ind)
        if (m == M[ind])break;
    return ind;
}

int main()
{
#ifdef _DEBUG
    ifstream cin("data.txt");
#endif // _DEBUG
    //                 0      1      2       3      4        5        6       7       8        9       10     11      12
    int a[13][2] = { { 0,0 },{ 31,31 },{ 28,29 },{ 31,31 },{ 30,30 },{ 31,31 },{ 30,30 },{ 31,31 },{ 31,31 },{ 30,30 },{ 31,31 },{ 30,30 },{ 31,31 } };   //一三五七八十腊,31天永不差,四六九东30天,唯有二月28,润二29
    string dofw[] = { "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday" };
    string Adofw[] = { "","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday" ,"Monday"};
    int time1 = 20180101, time2, y1 = 0, y2 = 0, m1 = 0, m2 = 0, d1 = 0, d2 = 0, day = 0, i, tmp; //time1默认为20180108  星期一
    int d, y; string m;
    bool flag = false;//标记输入日期与默认日期的大小,默认默认时间比输入时间小
    while ((cin >> d >> m >> y))
    {
        time2 = d + MstringToI(m) * 100 + y * 10000;
        day = 0; time1 = 20180101; flag = false;
        if (time1 > time2)
        {
            tmp = time1;
            time1 = time2;
            time2 = tmp;
            flag = true;
        }
        y1 = time1 / 10000; m1 = (time1 % 10000) / 100; d1 = time1 % 100;
        y2 = time2 / 10000; m2 = (time2 % 10000) / 100; d2 = time2 % 100;
        if (m1 != m2) //如果m1!=m2   计算y1对应m1那个月的天数和 y2对应m2那个月的天数
        {
            i = isLeap(y1) ? 1 : 0;
            day += a[m1][i] - d1;
            day += d2;
        }
        if (y1 < y2) //计算y1和y2两年不同年份的天数
        {
            i = isLeap(y1) ? 1 : 0;
            if (m1 == m2)
                day += a[m1][i] - d1 + d2;
            for (int k = m1 + 1; k < 13; ++k)
                day += a[k][i];
            i = isLeap(y2) ? 1 : 0;
            for (int k = 1; k < m2; ++k)
                day += a[k][i];
            for (int k = y1 + 1; k < y2; ++k)
                day += isLeap(k) ? 366 : 365;
        }
        else //同一年
        {
            i = isLeap(y1) ? 1 : 0;
            for (int k = m1 + 1; k < m2; ++k)
                day += a[k][i];
            if (m1 == m2)
                day += d2 - d1;
        }
        if (day > 6)day %= 7;
        if (flag)
            cout << Adofw[7 - day] << endl;
        else
            cout << dofw[day] << endl;
    }


#ifdef _DEBUG
    cin.close();
    system("pause");
#endif // _DEBUG

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Write a C++ program that defines a class DateV2 that (1) Contains all the members in the class DateV1; Programming for Engineers C++ (2) Has two constructors as follows: One takes three parameters, int y, int m, int n; The other is the default constructor that takes no parameter (3) Has additional public member functions as follows: string getWeekDay(); // return the week day, for example, Sunday if day is 0, etc bool Leap(); // return if the year is leap int differFrom(DateV2& oneDate); // return the difference in days between the calling object // and the oneDate object void printDate(); // print the year, the month in English, the day, and the week day Test class DateV2 in the main function as follows: (1) Declare and set the objects today and tomorrow as in Problem 2. (2) Declare and initialize (by a constructor) an object to represent your OWN birthday. (3) Use the member function printDate to print today, tomorrow, and your birthday. (4) Output the weekday of today, tomorrow, and your own birthday. (5) Output how many days has passed since your birth (the difference between your birthday and today). Hint: i) We can use another string array to store the English name for week days (Sunday, Monday, through Saturday) ii) We know that it is Monday on Year 1, Month 1, and Day 1 iii) A good idea is to first design a function to compute the number of days that has passed since Year 1, Month 1, and Day 1, and then to use this function to compute the week day for a give date and to compute the difference between two dates. You can store the number of days for each of the 12 months in an integer array, which helps in counting the days.
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值