如何填充闰年的元素

How to populate elements for leap year

本文关键字:闰年 元素 填充 何填充      更新时间:2023-10-16

我有一个非常简单的问题,但似乎无法绕开它或想出一些可行的方法。我想做的只是填充一个包含 100 个元素的数组来存储闰年。所以我将从 1804 年开始,当然会增加 4 年,因为闰年是每 4 年一次。代码真的很小。

#include <iostream>
using namespace std;
int main()
{
int month = 0;
int day = 0;
int year = 0;
int leapyear[100];
cout << "Please enter a date of birth in the format MMDDYYYY, month,day,and year should be seperate, so press enter after entering the numbers: ";
cin >> month;
cin >> day;
cin >> year;
int leapyear = 1804;
for (int i = 0; i < 99; i++)
{
leapyear [i] + 4;  //I know this can not be correct, what can I do here?  
}
for (int i = 0; i < 99; i++)
{
cout << leapyear[i];
}

您需要分配给leapyear数组。像这样:

for (int i = 0, curyear = 1904; i < 100; i++, curyear += 4) {
// skip century years, unless divisible by 400
if (curyear % 100 == 0 && curyear % 400 != 0) {
continue;
}
leapyear[i] = curyear;
}