需求
中国的天干地支记年法中,天干包含十大元素:甲 乙 丙 丁 戊 己 庚 辛 壬 癸
地支包含十二大元素:子 丑 寅 卯 辰 巳 午 未 申 酉 戌 亥
假设有天干数组:(1,2,3,4,5,6,7,8,9,10)、地支数组:(1,2,3,4,5,6,7,8,9,10,11,12)
天干地支的数组指针同时向前移动得到(1,1),(2,2)……(10,10)
天干数组的指针移动到末尾元素10会回到第一个元素1,地支数组的指针移动到末尾元素12会回到第一个元素1,
得到(1,11),(2,12),(3,1),(4,2)……
完整可表达为:
(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10)
(1,11),(2,12),(3,1),(4,2),(5,3),(6,4),(7,5),(8,6),(9,7),(10,8)
(1,9),(2,10),(3,11),(4,12),(5,1),(6,2),(7,3),(8,4),(9,5),(10,6),
(1,7),(2,8),(3,9),(4,10),(5,11),(6,12),(7,1),(8,2),(9,3),(10,4),
(1,5),(2,6),(3,7),(4,8),(5,9),(6,10),(7,11),(8,12),(9,1),(10,2),
(1,3),(2,4),(3,5),(4,6),(5,7),(6,8),(7,9),(8,10),(9,11),(10,12)
(1,1),(2,2),(3,3)……
可见循环60次(哈哈,当前代码中不能直接写死了)后,(1,1)会再次重复,即每60算一个甲子轮回
输出结果
(甲子),(乙丑)
(丙寅),(丁卯)
(戊辰),(己巳)
(庚午),(辛未)
(壬申),(癸酉)
(甲戌),(乙亥)
(丙子),(丁丑)
(戊寅),(己卯)
(庚辰),(辛巳)
(壬午),(癸未)
(甲申),(乙酉)
(丙戌),(丁亥)
(戊子),(己丑)
(庚寅),(辛卯)
(壬辰),(癸巳)
(甲午),(乙未)
(丙申),(丁酉)
(戊戌),(己亥)
(庚子),(辛丑)
(壬寅),(癸卯)
(甲辰),(乙巳)
(丙午),(丁未)
(戊申),(己酉)
(庚戌),(辛亥)
(壬子),(癸丑)
(甲寅),(乙卯)
(丙辰),(丁巳)
(戊午),(己未)
(庚申),(辛酉)
(壬戌),(癸亥)
实现
——循环的次数:等于两数组长度的最小公倍数。
public static void main(String[] args) {
String [] s1={"甲","乙","丙","丁", "戊","己","庚","辛","壬", "癸"};
String [] s2={"子","丑","寅","卯 ","辰","巳", "午", "未", "申", "酉", "戌", "亥"};
int m=s1.length;
int n=s2.length;
int max = (m > n)? m : n;
int count;//用于计算最小公倍数
for(count = max;count <= m * n;count++) {
if((count % m == 0)&&(count % n == 0)) {
//System.out.println("最小公倍数为:" + count);
break;
}
}
for(int i=0;i<count;i++){
System.out.println("("+s1[i%s1.length]+","+s2[i%s2.length]+")");
}
}
写作不易,既然来了,不妨点个关注,点个赞吧!!!