在特定年份和leap年中查找特定日期

时间:2019-06-09 17:03:44

标签: c++

所以,我已经完成了很多程序,我仍然必须确定当年1月1日是星期几以及leap年::年是其数可以被4整除的一年。但是,如果世纪可以精确地除以400,则只有leap年。因此1900年不是a年,而2000年才是a年。我有点想从这里去哪里,我脑子里很明白,但是很难将想法付诸代码。如果有人可以将我推向正确的方向,或者您有解决方案ID,则非常感谢您的帮助。

#include <ctime>
#include <iostream>
using namespace std;

int main()
{   
    tm dateTime;        
    time_t systemTime = time(0);        
    localtime_s( &dateTime, &systemTime ); 
    int day = dateTime.tm_mday;//theTime.tm_mday;
    int month = dateTime.tm_mon+1;//theTime.tm_mon;
    int year = dateTime.tm_year + 1900;//theTime.tm_year + 1900;
    int weekDay  = dateTime.tm_wday;
    cout << "Today is ";
    switch (weekDay){
        case 0: cout << "Sunday, ";
            break;
        case 1: cout << "Monday, ";
            break;
        case 2: cout << "Tuesday, ";
            break;
        case 3: cout << "Wednesday, ";
            break;
        case 4: cout << "Thursday, ";
            break;
        case 5: cout << "Friday, ";
            break;
        case 6: cout << "Saturday, ";
            break;
    }
    cout << month << "/" << day << "/" << year << endl;
}

3 个答案:

答案 0 :(得分:1)

  1. 使用modulo arithmetic operator%)来确定年份是否可以除以4。
    • 如果不是,那就不是飞跃。
  

请注意,当且仅当lhs可被rhs除以时,operator%的结果等于0。

  1. 然后,按照问题中的描述,将相同的运算符应用于alghoritm背后的逻辑,该逻辑确定年份是否为leap年。 细节在我的答案代码的注释中。
[[nodiscard]]
constexpr bool IsLeap(const int & Year) noexcept
{
    // If $Year is not dividable by 4, it's not leap, eg 2003.
    // Otherwise, apply more logic below (for years like 2004, 2000 or 1900).

    if (Year % 4 != 0) [[likely]]
        return false;

    // If $Year is dividable by 100, eg 2000, check if it's also dividable by 400.
    // If it's also dividable by 400, it's leap, eg 2000.
    // Otherwise, it's not leap, eg 1900.

    if (Year % 100 == 0) [[unlikely]]
    {
        if (Year % 400 == 0) [[unlikely]]
            return true;

        return false;
    }

    // $Year is dividable by 4 and not by 100, so it's leap.

    return true;
}

示例:

#include <iostream>
int main()
{
    std::cout << std::boolalpha << IsLeap(2003) << std::endl; // false (not dividable by 4)
    std::cout << std::boolalpha << IsLeap(2004) << std::endl; // true  (dividable by 4 and not dividable by 100)
    std::cout << std::boolalpha << IsLeap(2000) << std::endl; // true  (dividable by 4, 100 and 400)
    std::cout << std::boolalpha << IsLeap(1900) << std::endl; // false (dividable by 4 and 100, but not by 400)
}

答案 1 :(得分:0)

要检查给定数字是否可被另一个数字整除,请使用模数(%)运算符。如果为a % b == 0,则表示ab整除。

bool is_leap_year(int year) {
    if (year % 4 != 0) return false;
    // Year is divisible by 4; It is a leap year
    if (year % 100 == 0) {
        // Unless it is also divisible by 100, in which case it is not a leap year
        // Except when it is divisible by 400
        if (year % 400 == 0) return true;
        return false;
    }
    return true;
}

// Equivalent to
bool is_leap_year(int year) {
    return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}

然后,找出当前日期为1月1日的星期几,您将不得不再次使用模运算符。这次,您将使用a % 7,它将除以7后的余数(因此15天前是15 % 7 == 1天在另一个星期内)。

答案 2 :(得分:0)

使用纪元作为参考日期(我们可以选择任何日期),我们只需要计算当前日期即可得出星期几。

一种优化方法是,我们可以先增加年份,然后将dayOfTheWeek变量增加1或2,具体取决于我们当前是否在计算a年。

bool isLeapYear(int year)
{
    if ((year % 400) == 0)
        return true;
    if ((year % 100) == 0)
        return false;
    return ((year % 4) == 0);
}

// return the day of the week for a given month/day/year value
// Sunday is 0.  Monday is 1.... Saturday is 6;
int GetDayOfWeek(int month, int day, int year)
{
   int dayOfWeek = 5; // January 1, 1970 was a Thursday
   int daysOfMonth = [0,31,28,31,30,31,30,31,31,30,31,30,31];
   int d = 1;
   int m = 1;
   int y = 1970;

   if (year < 1970)
     return -1;

   // go "year by year" incrementing dayOfWeek by 1 or 2 based on leap year
   while (y < year)
   {
       dayOfWeek = (isLeapYear(y) ? 2 : 1) % 7;
       y++;
   }

   while (d != day && m != month)
   {
      // increment the day
      d++;
      dayOfWeek = (dayOfWeek + 1) % 7;
      if (d > daysOfMonth[m]) // new month
      {
          d = 1;
          m++;
      }
   }

   return dayOfWeek;
}