题目描述:
Description
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.
Input
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.
Output
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.
Sample Input
3 10. zac 0 0. pop 0 10. zac 1995
Sample Output
3 3 chuen 0 1 imix 0 9 cimi 2801
思路:
玛雅使用两种日历,第一种称为Haab,一年为365天,一年分为19个月,前18个月每个月都是20天,第19个月为5天,每一个月都有一个名字,分别为pop, no, zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan, pax, koyab, cumhu和uayet,每个月份中的天数用0到19表示。最后一个月份中的天数用0到4表示。玛雅使用的第二个日历称为Tzolkin,在这个日历中,一年为260天,一年分为13段,每段20天(260=13*20),每一天用一个数字和一个名字表示,总共20个名字: imix, ik, akbal, kan, chicchan, cimi, manik, lamat, muluk, ok, chuen, eb, ben, ix, mem, cib, caban, eznab, canac, ahau,数字为1到13,数字和名字都顺序循环,例如,第一年开始的几天为: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, 然后进入下一段 8 imix, 9 ik, 10 akbal...
现在的任务是要将一个Haab日历格式的日期转换为Tzolkin日历格式的日期。年份从0开始,从日期开始点开始,第一天在两种日历中分别为:
Haab:0. pop 0
Tzolkin: 1 imix 0
解题思路:
1 先将输入的日期转换为从第一天到该日期的总共天数:
days= year*365+month*20+date+1
2 根据days求出Tzolkin日历格式的年份,月份,天数
year = days/260;
Tz_days = days%260; //最后那年的天数
数字=Tz_days%13; (如果为0的话,应当是13)
天名字下标=Tz_days%20;(如果是0的话,应该是20)
注意:这里有一个坑,如果Tz_days==0的话,证明最后那年的天数为0,那这个日期就是上一年的最后一天,所以年数要减1
证明这个例子的测试用例:
> 4. uayet 259
> 正解是 13 ahau 364
> 而不是13 ahau 365
代码:
#include <stdio.h>
#include <string>
#include <map>
using namespace std;
map<string, int> ha_map;
void calculateDate(int date,string name,int year,string tz_map[]){
int days=year*365;
days+=ha_map[name]*20+date+1;
int Tz_year=days/260;
int Tz_days=days%260;
if(Tz_days==0)
Tz_year--;
int Tz_date=Tz_days%13;
if(Tz_date==0)
Tz_date=13;
int Tz_month=Tz_days%20;
if(Tz_month==0)
Tz_month=20;
string Tz_name=tz_map[Tz_month];
printf("%d %s %d\n",Tz_date,Tz_name.c_str(),Tz_year);
}
int main(){
ha_map["pop"]=0;
ha_map["no"]=1;
ha_map["zip"]=2;
ha_map["zotz"]=3;
ha_map["tzec"]=4;
ha_map["xul"]=5;
ha_map["yoxkin"]=6;
ha_map["mol"]=7;
ha_map["chen"]=8;
ha_map["yax"]=9;
ha_map["zac"]=10;
ha_map["ceh"]=11;
ha_map["mac"]=12;
ha_map["kankin"]=13;
ha_map["muan"]=14;
ha_map["pax"]=15;
ha_map["koyab"]=16;
ha_map["cumhu"]=17;
ha_map["uayet"]=18;
string tz_map[]={"0","imix","ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"};
int n;
scanf("%d",&n);
printf("%d\n",n);
for(int i=0;i<n;i++){
int date;
int year;
char name2[10];
scanf("%d. %s %d",&date,&name2,&year);//这里不能直接写name,即&后不能为string类型,必须是char[],否则赋值不正确
string name=name2;
calculateDate(date,name,year,tz_map);
}
return 0;
}
我这里是用了map进行匹配的,我看了下别人的做法,有人没有用map,就写了个函数,返回字符串数组中某个字符串的下标。这样也未尝不可,时间差不多其实。
#include<iostream>
#include<string>
using namespace std;
string m[19]={"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu","uayet"};
string d[20]={"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"};
int summons(string hm)
{
for(int i=0;i<19;i++)
{
if(hm==m[i])
{
return i;
}
}
return -1;
}
int main()
{
long n,i,j,k;
cin>>n;
cout<<n<<endl;
for(i=0;i<n;i++)
{
long hd;
string hm;
long hy;
cin>>hd>>hm>>hm>>hy;
long hm1=summons(hm);
long hdays=0;
hdays=hy*365+20*hm1+hd;
long y=hdays/260;
long days=hdays%260;
cout<<days%13+1<<" "<<d[days%20]<<" "<<y<<endl;
}
return 0;
}