有个关于天干地支的计算方法,参考于百度百科
(年份- 3)/10余数对天干:如1894-3=1891 ,1891除以10余数是1即为甲;
(年份- 3)/12余数对地支:如1894-3=1891 ,1891除以12余数是7即为午,即1894年是甲午年。
因代码中的甲和子是从下标0开始的,所以年份减4;
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
string s;
int n,k;
struct node{
string st;
int len;
}st[N];
int j;
string tg[10] = {"jia","yi","bing","ding","wu","ji","geng","xin","ren","gui"};
string dz[12] ={"zi","chou","yin","mao","chen","si","wu","wei","shen","you","xu","hai"};
int main(){
int n;
cin >> n;
int flag = 1;
cout << tg[(n - 4)%10];
cout << dz[(n - 4)%12];
return 0;
}
含错误思路的代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
string s;
int n,k;
//string st[N];
struct node{
string st;
int len;
}st[N];
int j;
string tg[10] = {"jia","yi","bing","ding","wu","ji","geng","xin","ren","gui"};
string dz[12] ={"zi","chou","yin","mao","chen","si","wu","wei","shen","you","xu","hai"};
int main(){
int n;
cin >> n;
// vector<int> c;
int flag = 1;
// while(n){
// n %= 10;
cout << tg[(n - 4)%10];
// n -= n/10;
cout << dz[(n-4)%12];
// n /= 12;
// n %= 12;
// if(flag){
// cout << dz[n/60%10];
c.push_back(n%12);
// n/=10;
// flag = 0;
// }
// else{
// cout << tg[n%12];
c.push_back(n%10);
// n/=12;
// flag = 1;
// }
// }
return 0;
}