Determining the nearest upcoming Chinese solar term requires mapping a given Gregorian month and day to a fixed chronological sequence. The operational cycle spans from early February of one year through late January of the subsequent year, with dates on or after February 4 assigned to the primary cycle and earlier dates mapping to the following year. Input consists of two integers representing the month and day, and the required output is a two-character uppercase abbreviation corresponding to the matched term.
Sequential conditional branching can be replaced with a structured lookup table and a linear scan. By normalizing January dates to a virtual thirteenth month, the timeline becomes strictly monotonic across the year boundary. Each record stores the onset month, day, and corresponding abbreviation. The algorithm compares the normalized input against each record sequentially and terminates upon locating the first threshold that meets or exceeds the provided date.
#include <iostream>
#include <vector>
struct SolarTerm {
int onset_month;
int onset_day;
const char* abbreviation;
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
const std::vector<SolarTerm> terms = {
{2, 4, "LC"}, {2, 19, "YS"}, {3, 5, "JZ"}, {3, 20, "CF"},
{4, 4, "QM"}, {4, 19, "GY"}, {5, 5, "LX"}, {5, 20, "XM"},
{6, 5, "MZ"}, {6, 21, "XZ"}, {7, 6, "XS"}, {7, 22, "DS"},
{8, 7, "LQ"}, {8, 22, "CS"}, {9, 7, "BL"}, {9, 22, "QF"},
{10, 8, "HL"}, {10, 23, "SJ"}, {11, 7, "LD"}, {11, 22, "XX"},
{12, 7, "DX"}, {12, 21, "DZ"}, {1, 5, "XH"}, {1, 20, "DH"}
};
int month, day;
if (!(std::cin >> month >> day)) return 0;
int normalized_input = (month == 1 ? 13 : month) * 100 + day;
for (const auto& term : terms) {
int normalized_term = (term.onset_month == 1 ? 13 : term.onset_month) * 100 + term.onset_day;
if (normalized_term >= normalized_input) {
std::cout << term.abbreviation << "\n";
break;
}
}
return 0;
}
The normalization technique converts month-day pairs into comparable integer values, eliminating complex date arithmetic and cross-year boundary checks. Iterating through the chronologically sorted array ensures that the first qualifying entry corresponds to the nearest upcoming solar term. Memory consumption remains fixed, and execution time scales linearly with the number of predefined terms, providing deterministic performance without recursive or nested conditional overhead.