Maya Calendar
Description 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. Input 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. Output 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. Sample Input 3
10. zac 0
0. pop 0
10. zac 1995
Sample Output 3
3 chuen 0
1 imix 0
9 cimi 2801
Source |
首先计算某Haab历距离世界开始时的天数,再通过计算得到的天数,转换成相应的Tzolkin日期。
1 #include<iostream> 2 #include<cstring> 3 #include<map> 4 #include<stdio.h> 5 #include<string> 6 #include<stdio.h> 7 using namespace std; 8 char HaabMonth[20][10] = {"pop", "no", "zip", "zotz", 9 "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", 10 "mac", "kankin", "muan", "pax", "koyab", "cumhu","uayet"}; 11 char TzolkinMonth[20][10] = {"imix", "ik", "akbal", "kan", "chicchan", 12 "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", 13 "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"}; 14 struct HaabCal 15 { 16 int day; 17 char month[10]; 18 int year; 19 }; 20 struct TzolkinCal 21 { 22 int Number; 23 char NameOfTheDay[10]; 24 int Year; 25 }; 26 //计算Haab是第几天 27 long DaysOfHaab(HaabCal h) 28 { 29 long days=0; 30 days += 365*h.year + h.day + 1;//Haab的天数从0开始计 31 //加入月份 32 int Month; 33 for(int i=0;i<18;i++) 34 { 35 if(strcmp(HaabMonth[i],h.month) == 0) 36 {Month = i;break;} 37 else Month = 18; 38 } 39 days += 20*Month; 40 return days; 41 } 42 //将天数转换为TzolkinCal 43 void printTzolkin(long days) 44 { 45 TzolkinCal t; 46 t.Year = (days-1)/260;//年分从0开始记 47 int temp = (days-1)%260+1;//一年中的第几天 48 t.Number = (temp-1)%13+1; 49 int NaOfDay = (temp-1)%20; 50 strcpy(t.NameOfTheDay,TzolkinMonth[NaOfDay]); 51 printf("%d %s %d ",t.Number,t.NameOfTheDay,t.Year); 52 } 53 int main() 54 { 55 int n=0; 56 cin>>n; 57 cout<<n<<endl;; 58 HaabCal MyHaab; 59 while(n>0) 60 { 61 scanf("%d.%s %d",&MyHaab.day,&MyHaab.month,&MyHaab.year); 62 int days = DaysOfHaab(MyHaab); 63 printTzolkin(days); 64 n--; 65 } 66 return 0; 67 }
写给自己:
1)cannot pass object of non-POD type 'string'(aka 'basic_string<char>')through variadic function
在代码中使用了类似"%s"等格式化来处理string类型的时候,出现:
cannot pass object of non-POD type 'string'(aka 'basic_string<char>')through variadic function
这样的错误,百度下发现:
printf,scanf,fprinf等可以format的一个字符串中使用"%s"时,只能使用C string;如果是C++ string的话,就必须先变成C string,否则就会出现类似上面的错误.
示例代码:
string str ("Test string"); printf("%s ", str);
这个时候会出现编译错误:
warning:cannot pass objects of non-POD type 'struct std::string' through '...';call will abort at runtime.
此时,只需要将C++ string转化成 c string就可以了,如:
str.c_str();
2)遇到的一个错误
如果代码这样写:
1 /* 2 这是错误示例 3 */ 4 #include<iostream> 5 #include<cstring> 6 #include<map> 7 #include<stdio.h> 8 #include<string> 9 #include<stdio.h> 10 using namespace std; 11 string HaabMonth[20] = {"pop", "no", "zip", "zotz", 12 "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", 13 "mac", "kankin", "muan", "pax", "koyab", "cumhu","uayet"}; 14 string TzolkinMonth[20] = {"imix", "ik", "akbal", "kan", "chicchan", 15 "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", 16 "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"}; 17 struct HaabCal 18 { 19 int day; 20 string month; 21 int year; 22 }; 23 struct TzolkinCal 24 { 25 int Number; 26 string NameOfTheDay; 27 int Year; 28 }; 29 //计算Haab是第几天 30 long DaysOfHaab(HaabCal h) 31 { 32 long days=0; 33 days += 365*h.year + h.day + 1;//Haab的天数从0开始计 34 //加入月份 35 int Month; 36 for(int i=0;i<18;i++) 37 { 38 if(HaabMonth[i] == h.month) 39 {Month = i;break;} 40 else Month = 18; 41 } 42 days += 20*Month; 43 return days; 44 } 45 //将天数转换为TzolkinCal 46 void printTzolkin(long days) 47 { 48 TzolkinCal t; 49 t.Year = (days-1)/260;//年分从0开始记 50 int temp = (days-1)%260+1;//一年中的第几天 51 t.Number = (temp-1)%13+1; 52 int NaOfDay = (temp-1)%20; 53 t.NameOfTheDay = TzolkinMonth[NaOfDay]; 54 printf("%d %s %d ",t.Number,t.NameOfTheDay.c_str(),t.Year); 55 } 56 int main() 57 { 58 int n=0; 59 cin>>n; 60 cout<<n<<endl;; 61 HaabCal MyHaab; 62 while(n>0) 63 { 64 scanf("%d.%s %d",&MyHaab.day,&MyHaab.month,&MyHaab.year); 65 int days = DaysOfHaab(MyHaab); 66 printTzolkin(days); 67 n--; 68 } 69 return 0; 70 }
会发生如下错误:
也就是上述的对结构体内String类型的赋值方法会引起程序中断,结构体内的string不定长,没有给他分配内存,需要用用new来分配内存(不能用malloc,malloc不会调用结构函数)
如上可以运行通过,当然不满足题设要求。
这里改为char。
只要是指针,要使用它前就必须保证指针变量的值是一个有效的值;否则,它指向的内存一定是垃圾数据!
3)string的使用
string中的assign赋值函数
1 string &operator=(const string &s);//把字符串s赋给当前字符串 2 string &assign(const char *s);//用c类型字符串s赋值 3 string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值 4 string &assign(const string &s);//把字符串s赋给当前字符串 5 string &assign(int n,char c);//用n个字符c赋值给当前字符串 6 string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串 7 string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串
int strcmp ( const char * str1, const char *str2 );
strcmp是字符串比较函数,作用是比较字符串1和字符串2
如:strcmp(str1,str2);
strcmp("china","korea");
比较的结果由函数带回。
(1)如果字符串1=字符串2,函数值为0。
(2)如果字符串1〉字符串2,函数值为一正整数
(3)如果字符串1<字符串2,函数值为一负整数
char * strcpy ( char * destination, const char * source );
用法:#include <string.h>
功能:把src所指由NULL结束的字符串复制到dest所指的数组中。
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。