题目描述:
在我国古代和近代,一直采用干支法纪年。它采用10天干和12地支配合,一个循环周期为60年。
10天干是:甲,乙,丙,丁,戊,己,庚,辛,壬,癸
12地支是:子,丑,寅,卯,辰,巳,午,未,申,酉,戌,亥
如果某年是甲子,下一年就是乙丑,再下是丙寅,......癸酉,甲戌,乙亥,丙子,....
总之天干、地址都是循环使用,两两配对。
今年(2012)是壬辰年,1911年辛亥革命
下面的代码根据公历年份输出相应的干支法纪年。已知最近的甲子年是1984年。
程序分析:
关键点是题目中的“循环使用,两两配对”这句话,循环使用就是除k取余
程序代码:
void f(int year)
{
char* x[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
char* y[] = {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
int n = year - 1984;
while(n<0) n += 60;
printf("%s%s\n", x[n%10], y[n%12]);
}
int main(int argc, char* argv[])
{
f(1911);
f(1970);
f(2012);
return 0;
}
程序输出:
辛亥
庚戌
壬辰
我们还可以通过下面的程序,输出一个循环周期:
#include<iostream>
using namespace std;
int main()
{
char* x[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
char* y[] = {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
int n=0;
while(n+1<61)
{
cout<<n+1<<"";
cout<<x[n%10]<<y[n%12]<<endl;
n++;
}
}
程序输出:
1 甲子
2 乙丑
3 丙寅
4 丁卯
5 戊辰
6 己巳
7 庚午
8 辛未
9 壬申
10 癸酉
11 甲戌
12 乙亥
13 丙子
14 丁丑
15 戊寅
16 己卯
17 庚辰
18 辛巳
19 壬午
20 癸未
21 甲申
22 乙酉
23 丙戌
24 丁亥
25 戊子
26 己丑
27 庚寅
28 辛卯
29 壬辰
30 癸巳
31 甲午
32 乙未
33 丙申
34 丁酉
35 戊戌
36 己亥
37 庚子
38 辛丑
39 壬寅
40 癸卯
41 甲辰
42 乙巳
43 丙午
44 丁未
45 戊申
46 己酉
47 庚戌
48 辛亥
49 壬子
50 癸丑
51 甲寅
52 乙卯
53 丙辰
54 丁巳
55 戊午
56 己未
57 庚申
58 辛酉
59 壬戌
60 癸亥