C 中的古代日本日历练习

Ancient Japanese calendar exercise in C++

本文关键字:日本 日历 练习      更新时间:2023-10-16

练习:

在古代日本日历中是60年的周期。每年的数量从1到60,分为成对,每种都有自己的颜色(绿色,红色,黄色,白色或黑色)。年度的颜色分布如下:

  • 1,2,2,11,12,21,22,...,51,52年 - 绿色;
  • 3,4,13,14,23,24,...,53,54年 - 红色;
  • 5、6、6、15、16、25、26,...,55、56年 - 黄色;
  • 7、8、17、18、27、28,...,57、58年 - 白色;
  • 9、10、19、20、29、30,...,59、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;
}