目录
Maya Calendar
要求:
总时间限制: 1000ms
内存限制: 65536kB
描述:
During his last sabbatical(休假), professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted(打结的) message, professor discovered that the Maya civilization used a 365 day long year, called Haab, which had 19 months. Each of the first 18 months was 20 days long, and the names of the months were pop, no, zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan, pax, koyab, cumhu. Instead of having names, the days of the months were denoted(表示) by numbers starting from 0 to 19. The last month of Haab was called uayet and had 5 days denoted by numbers 0, 1, 2, 3, 4. The Maya believed that this month was unlucky, the court of justice(法院) was not in session, the trade stopped, people did not even sweep the floor. For religious purposes, the Maya used another calendar in which the year was called Tzolkin (holly year). The year was divided into thirteen periods, each 20 days long. Each day was denoted by a pair consisting of a number and the name of the day. They used 20 names: imix, ik, akbal, kan, chicchan, cimi, manik, lamat, muluk, ok, chuen, eb, ben, ix, mem, cib, caban, eznab, canac, ahau and 13 numbers; both in cycles. Notice that each day has an unambiguous description. For example, at the beginning of the year the days were described as follows: 1 imix, 2 ik, 3 akbal, 4 kan, 5 chicchan, 6 cimi, 7 manik, 8 lamat, 9 muluk, 10 ok, 11 chuen, 12 eb, 13 ben, 1 ix, 2 mem, 3 cib, 4 caban, 5 eznab, 6 canac, 7 ahau, and again in the next period 8 imix, 9 ik, 10 akbal . . . Years (both Haab and Tzolkin) were denoted by numbers 0, 1, . . . , where the number 0 was the beginning of the world. Thus, the first day was: Haab: 0. pop 0 Tzolkin: 1 imix 0 Help professor M. A. Ya and write a program for him to convert the dates from the Haab calendar to the Tzolkin calendar.
输入:
The date in Haab is given in the following format:
NumberOfTheDay. Month Year
The first line of the input file contains the number of the input dates in the file. The next n lines contain n dates in the Haab calendar format, each in separate line. The year is smaller then 5000.
输出:
The date in Tzolkin should be in the following format:
Number NameOfTheDay Year
The first line of the output file contains the number of the output dates. In the next n lines, there are dates in the Tzolkin calendar format, in the order corresponding to the input dates.
样例输入:
3 10. zac 0 0. pop 0 10. zac 1995
样例输出:
3 3 chuen 0 1 imix 0 9 cimi 2801
问题分析:
根据题意,我们要帮助Professor M. A. Ya写一个程序,以便让Haab的日历格式转换为Tzolkin的日历格式。两种日历格式的具体计时方法已经在题中给出,简单来说,Haab的一年有365天,19个月,前18个月一个月20天,最后一个月只有5天;而Tzolkin的一年有260天,没有月的概念,但是每天都有自己的标记,由一个数字以及一个名字(name)组成。
所以我们需要先根据Haab记法算出当下时间,再通过Tzolkin记法进行转换,必不可少的就是利用const string将Haab与Tzolkin中的名称转换为数字标号以便计算。
在计算时需要注意的是:Haab记法中,天数是从0到19,而非1到20,所以不要忘了加1;在计算Tzolkin日期时,不能直接采用(%13)或(%20)的方法,因为13%13=0,所以我们可以考虑(13-1)%13+1=13即可;在计算Tzolkin年的时候,也不能直接采用(/260)的方式,因为260/260=1,而实际上第260天还在第一年中,所以我们考虑(260-1)/260=0.
其次,我们可以考虑使用结构体来使程序更加简洁易懂.
最终代码:
#include<iostream>
using namespace std;
const string HaabMonth[20]={"","pop","no","zip","zotz","tzec","xul","yoxkin","mol","chen","yax","zac","ceh","mac","kankin","muan","pax","koyab","cumhu","uayet"};
const string TzolkinName[21]={"","imix","ik","akbal","kan","chicchan","cimi","manik","lamat","muluk","ok","chuen","eb","ben","ix","mem","cib","caban","eznab","canac","ahau"};
//空出一位,符合平时习惯
struct Haab{
int day;
char month[7];//便于利用scanf输入
int year;
};
struct Tzolkin{
int number{};
string name;
int year{};
};
int main(){
int n;scanf("%d",&n);
printf("%d\n",n);
for(int i=0;i<n;i++){
Haab Date{};Tzolkin Ans;
scanf("%d. %s %d",&Date.day,Date.month,&Date.year);
int num=Date.year*365+Date.day+1;
for(int j=1;j<=19;j++){
if(HaabMonth[j]==Date.month){
num+=(j-1)*20;
break;
}
}//搜索找到对应的月份,其实可以利用map来进行简化,但是对此题没有太大必要
Ans.year=(num-1)/260;
Ans.number=(num-1)%13+1;
Ans.name=TzolkinName[(num-1)%20+1];//这三行是精髓所在(-1与+1的运用)
printf("%d %s %d\n",Ans.number,Ans.name.c_str(),Ans.year);
}
}
总结:
这道题目总体来说不难,但是还是有一些需要注意的点,尤其是最后+1与-1的运用,尤为重要,可以省去一些不必要的判断语句,而且这些地方也是易错点,稍有疏忽就会出错。如果这篇文章对您有所帮助的话,请不要忘了点赞关注哦!