POJ-1008 Maya Calendar

【题目描述】

将Haab历法的日期转化为Tzolkin历法,前者一年365天,后者一年260天。(简直比我们的历法转换还简单,都没闰年!)

【思路分析】

简单的思路就是将Haab历法的日期转换成从第一天开始的总天数,再转换成Tzolkin历法。由于最大年份为5000年,乘上365天也不会使int型越界,所以放心转吧。

做题过程中主要复习的知识点是:

1. STL中map的用法:

map<string, int> HaabMonth;     // 定义
HaabMonth.insert( pair<string,int>("pop", 0) );
                                // 插入
HaabMonth[hmonth] = 100;        // 查找及修改
HaabMonth.erase("ok");          // 删除

// 另一种查找方式
HaabMonth::iterator iter = HaabMonth.find("ok");
if (iter == HaabMonth.end())
{
    ...        // 则没有找到
}

// 遍历方法
for (HaabMonth::iterator iter = HaabMonth.begin(); iter != HaabMonth.end(); ++iter)
{
    ...
}

2. string 与 char* 的转换:

string到char*,string.c_str();char*到string,直接赋值string = char*;

另外:string转数字,用string.c_str() + atoi, atof…;

数字转string,用sprintf(char[], “%d”, int); 再将char*赋值给string;

【附:完整源码】

/*
* POJ-1008 Maya Calendar
*/
#include <iostream>
#include <map>
#include <string>
using namespace std;

void BuildCalendarMonth(map<string,int> &hm)
{
    hm.insert( pair<string,int>("pop", 0) );
    hm.insert( pair<string,int>("no", 20) );
    hm.insert( pair<string,int>("zip", 40) );
    hm.insert( pair<string,int>("zotz", 60) );
    hm.insert( pair<string,int>("tzec", 80) );
    hm.insert( pair<string,int>("xul", 100) );
    hm.insert( pair<string,int>("yoxkin", 120) );
    hm.insert( pair<string,int>("mol", 140) );
    hm.insert( pair<string,int>("chen", 160) );
    hm.insert( pair<string,int>("yax", 180) );
    hm.insert( pair<string,int>("zac", 200) );
    hm.insert( pair<string,int>("ceh", 220) );
    hm.insert( pair<string,int>("mac", 240) );
    hm.insert( pair<string,int>("kankin", 260) );
    hm.insert( pair<string,int>("muan", 280) );
    hm.insert( pair<string,int>("pax", 300) );
    hm.insert( pair<string,int>("koyab", 320) );
    hm.insert( pair<string,int>("cumhu", 340) );
    hm.insert( pair<string,int>("uayet", 360) );
}

int main()
{
    map<string, int> HaabMonth;
    BuildCalendarMonth(HaabMonth);
    string TzolkinMonth[20] = {"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik",
        "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"};

    int n;
    cin>>n;
    cout<<n<<endl;

    for (int i(0); i < n; i++)
    {
        int hday, hyear, hmonthdays;
        char temphmonth[10];
        string hmonth;
        scanf("%d. %s %d", &hday, temphmonth, &hyear);

        hmonth = temphmonth;
        hmonthdays = HaabMonth[hmonth];

        int totalDays = hyear * 365 + hmonthdays + hday;

        // 转化Tzolkin历法
        int tyear = totalDays / 260;
        int totalDaysInOneYear = totalDays % 260;
        int tmonthno = totalDaysInOneYear % 20;
        string tmonth = TzolkinMonth[tmonthno];
        int tday = totalDaysInOneYear % 13 + 1;

        cout<< tday <<" "<< tmonth <<" "<< tyear <<endl;
    }

    return 0;
}
时间: 2024-08-13 04:21:23

POJ-1008 Maya Calendar的相关文章

poj 1008:Maya Calendar(模拟题,玛雅日历转换)

Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message, profes

[POJ] #1008# Maya Calendar : 字符处理/同余问题

一. 题目 Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74085   Accepted: 22819 Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message,

POJ 1008 Maya Calendar 水模拟

有中文意思. 注意HOLLY法月份和天数是一起滚动的= = 要输出总组数,坑哭 #include <cstring> #include <cstdio> #include <algorithm> #include <map> #include <string> #include<iostream> using namespace std; map<string, int>f; const int N = 22; char

POJ 1008 Maya Calendar(模拟)

#include"cstdio" #include"iostream" #include"cstring" using namespace std; char H[19][10]={"pop","no","zip","zotz","tzec","xul","yoxkin","mol",&q

Poj OpenJudge 百练 Bailian 1008 Maya Calendar

1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66971   Accepted: 20644 Description During his last sabbatical, professor M. A. Ya

[POJ] POJ1008 Maya Calendar

该篇作为POJ之路的起点,初学乍道,请多指教. Problem: 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

Poj Maya Calendar

http://poj.org/problem?id=1008 Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64607 Accepted: 19908 Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an

B - 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 month

【poj1008】Maya Calendar

模拟,先算出天数,再转化成另一个历. #include<iostream> #include<cstdio> #include<cstring> using namespace std; char name[21][10] = {"0","imix","ik","akbal","kan","chicchan","cimi",&quo