C++中的古代日本日历练习

Ancient Japanese calendar exercise in C++

练习:

日本古代历法是60年一周期。每年从1到60被编号并分成一对,每一年都有自己的颜色(绿色,红色,黄色,白色或黑色)。年份的颜色分布如下:

我们知道新的 60 年周期从 1984 年开始,到 2043 年结束; 1984和1985是绿色年,1986和1987是红色年,2043年是黑色年

我们知道年份 m(1800 < m < 2200)。写一个程序,打印出当年的颜色。

P.S。这个练习不是用英文写的!

根据您的列表,颜色以 10 年为周期轮换,随后 2 年始终使用相同的颜色。根据该规则,您可以计算年份颜色字段中的索引。

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    const int base_year = 1984;
    vector<string> colors = { "green", "red", "yellow", "white", "black" };

    int year;
    cout << "Insert year: ";
    cin >> year;

    int offset = year - base_year;
    int index = (offset % 10) / 2;
    if (index < 0) index += colors.size();

    cout << "year offset " << offset << " color index :" << "year color: " << colors[index] << endl;

    return 0;
}