1. 程式人生 > >Maya Calendar

Maya Calendar

stdio.h program can enter struct rcp months tr1 賦值

Maya Calendar

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 80533 Accepted: 24745

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

Central Europe 1995

首先計算某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\n",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\n", 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\n",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的指針。
  
 

Maya Calendar