POJ1088 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

思路:

玛雅使用两种日历,第一种称为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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值