北大 1008 Maya Calendar——做到这里的一点心得

53 篇文章 0 订阅
36 篇文章 0 订阅
                                                                        Maya Calendar
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 60309 Accepted: 18596

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

下面是自己的翻译:

M.A教授在他最后一个度假时惊讶的发现了玛雅历中的一个现象。从一个古老未解的信息中,M.A教授发现玛雅文明用365天分为19个月表示一年,并称之为Haab。1-18月份分别命

名为pop,no, zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan,pax, koyab, cumhu每月20天。而在每个月的天

数中以0-19来区别。最后一个月份Haab 也叫做uayet 却只有5天分别以0-4来表示。玛雅人相信最后一个月份是一个灾难月,以致于在这个月份时司法法庭不会开庭,交易停止,

人们也不会打扫房间。

为了达到宗教的目的,玛雅人改用Tzolkin日历。将一年分为13个周期,每个周期20天。 每一天以数字和名字的组合来表示。他们使用的20个名字有:imix, ik, akbal, kan, chicchan, cimi, manik, lamat, muluk, ok,chuen, eb, ben, ix, mem, cib, caban, eznab, canac, ahau 和13个数字,以下的以此类推。 注意到每一天都有一个很清楚的描述。比如在一年中每天可以这样描述: 1 imix, 2 ik, 3 akbal, 4 kan, 5 chicchan, 6 cimi, 7 manik, 8 lamat, 9 muluk, 10ok, 11 chuen, 12 eb, 13 ben, 1 ix, 2 mem, 3 cib, 4 caban, 5 eznab, 6 canac, 7ahau, and again in the next period 8 imix, 9 ik, 10 akbal . . .
贴上自己的代码:

#include <iostream>
#include <string>

using namespace std;

class Data{	
public:
	Data(){};
	string numberOfTheDay;
	string month;
	 int year;
};

class Result {
public:
	Result(){};
	int day;
	string month;
	 int year;
};

int count_num = 0; //输入的数据对
Data* data;
Result *result;


int main()
{
	void InputMessage();
   long int CalculateDayForInput(Data data);
	string IntConvectString_new(int month);
	void OutputMessage();

	InputMessage();
	
	long  int days;
	for(int i = 0; i < count_num; i++)
	{
		days = CalculateDayForInput(data[i]);/*计算输出原始数据的总天数*/
	//	cout << days << endl;
		result[i].year = days / 260;
		int temp_month = 0;
		if(result[i].year == 0){
			 temp_month = (days % 260) / 20 + 1;
		}else{ 
			 temp_month = (days % 260) / 20;
		}
		result[i].month = IntConvectString_new(temp_month);
		result[i].day =(days % 260) % 13;
	}

	OutputMessage();

	int g;
	cin >> g;
	return 1;
}

void OutputMessage()
{
	cout << count_num << endl;
	for(int i = 0; i < count_num; i++)
	{
		cout <<  result[i].day << " " 
			<< result[i].month << " "
			<< result[i].year << endl;
	}
}

/*imix, ik, akbal, kan, chicchan, cimi, manik, lamat, muluk, ok, chuen, eb, ben, ix, mem, cib, caban, eznab, canac, ahau */
/*将月份数字转换成表示月份的字符串*/
//失败 返回NULL值
string IntConvectString_new(int month)
{
	if(month == 1)
		return "imix";
	else if(month == 2)
		return "ik";
	else if(month == 3)
		return "akbal";
	else if(month == 4)
		return "kan";
	else if(month == 5)
		return  "chicchan";
	else if(month == 6)
		return "cimi";
	else if(month == 7)
		return "manik";
	else if(month == 8)
		return "lamat";
	/*imix, ik, akbal, kan, chicchan, cimi, manik, lamat, muluk, ok, chuen, eb, ben, ix, mem, cib, caban, eznab, canac, ahau */
	else if(month == 9)
		return "muluk";
	else if(month == 10)
		return "ok";
	else if(month == 11)
		return "chuen";
	else if(month == 12)
		return "eb";
	else if(month == 13)
		return "ben";
	else if(month == 14)
		return "ix";
	else if(month == 15)
		return "mem";
	else if(month == 16)
		return "cib";
	else if(month == 17)
		return "caban";
	else if(month == 18)
		return "eznab";
	else if(month == 19)
		return "canac";
	else if(month == 20)
		return "ahau";

	return NULL;
}

/*succes return number represent month,else return 0*/
/*pop, no, zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan, pax, koyab, cumhu,Haab */
int MonthConvetForString_old(string str)
{
	if(str == "pop")
		return 1;
	else if(str == "no")
		return 2;
	else if(str == "zip")
		return 3;
	else if(str == "zotz")
		return 4;
	else if(str == "tzec")
		return 5;
	else if(str == "xul")
		return 6;
	else if(str == "yoxkin")
		return 7;
	else if(str == "mol")
		return 8;
	/*pop, no, zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan, pax, koyab, cumhu,Haab */
	else if(str == "chen")
		return 9;
	else if(str == "yax")
		return 10;
	else if(str == "zac")
		return 11;
	else if(str == "ceh")
		return 12;
	else if(str == "mac")
		return 13;
	else if(str == "kankin")
		return 14;
	else if(str == "muan")
		return 15;
	else if(str == "pax")
		return 16;
	else if(str == "koyab")
		return 17;
	else if(str == "cumhu")
		return 18;
	else if(str == "haab")
		return 19;
	else 
		return -1;
}

long int CalculateDayForInput(Data data)
{
	int month = MonthConvetForString_old(data.month) - 1; //将字串月份转化为数字
	int sum_month_day = 0;
	const int temp = atoi(data.numberOfTheDay.c_str());
	if(month == 19)
	{
		sum_month_day = 360 + temp;
	}else{
		sum_month_day = month*20 + temp;
	}
	return data.year*365 + sum_month_day + 1;
}

void InputMessage()
{
	cin >> count_num;
	data = new Data[count_num];
	result = new Result[count_num];	
	for(int i=0; i < count_num; i++)
	{
		cin >> data[i].numberOfTheDay >> data[i].month >> data[i].year;
		/*去除numberOfTheDay最后的"."*/
		data[i].numberOfTheDay.replace (data[i].numberOfTheDay.find('.'),data[i].numberOfTheDay.find('.'),"");
	}
}

慢慢当当的将近200行,而且在提交的时候出现“Runtime Error” 对于这个错误的解释,我觉得是在考虑时没有全面,出现了未知的情况,而你没有做处理,于是乎在运行中
出现了错误。做这个题目从看题翻译到最后的代码花了一天的时间,可是最终的结果却未偿我愿。不过当我在看到这份代码时,我惊呆了,我感觉我错了......

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
    string s;
    int y,day,md,d,s2,num,T;
    cin>>T;
    cout<<T<<endl;
    while(T--){
        scanf("%d.",&d);
        cin>>s>>y;
        if(s=="pop") md=0;
        if(s=="no") md=20;
        if(s=="zip") md=40;
        if(s=="zotz") md=60;
        if(s=="tzec") md=80;
        if(s=="xul") md=100;
        if(s=="yoxkin") md=120;
        if(s=="mol") md=140;
        if(s=="chen") md=160;
        if(s=="yax") md=180;
        if(s=="zac") md=200;
        if(s=="ceh") md=220;
        if(s=="mac") md=240;
        if(s=="kankin") md=260;
        if(s=="muan") md=280;
        if(s=="pax") md=300;
        if(s=="koyab") md=320;
        if(s=="cumhu") md=340;
        if(s=="uayet") md=360;

        day=d+md+y*365+1;

        y=day/260;
        day%=260;
        if(day==0) cout<<13<<" ahau "<<y-1<<endl;
        else{
        num=day%13;
        if(num==0) num=13;
        s2=day%20;
        if(s2==0) s2=20;
        switch(s2){
            case 1:s="imix";break;
            case 2:s="ik";break;
            case 3:s="akbal";break;
            case 4:s="kan";break;
            case 5:s="chicchan";break;
            case 6:s="cimi";break;
            case 7:s="manik";break;
            case 8:s="lamat";break;
            case 9:s="muluk";break;
            case 10:s="ok";break;
            case 11:s="chuen";break;
            case 12:s="eb";break;
            case 13:s="ben";break;
            case 14:s="ix";break;
            case 15:s="mem";break;
            case 16:s="cib";break;
            case 17:s="caban";break;
            case 18:s="eznab";break;
            case 19:s="canac";break;
            case 20:s="ahau";break;
        }
        cout<<num<<' '<<s<<' '<<y<<endl;
        }
    }
}

只有70行,其中的代码即便没有注释,也一看就明了.....

我觉得在代码的编写过程中一定要注意精炼二字!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值