标签:style blog io ar color os sp for strong
【题目描述】
将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; }
标签:style blog io ar color os sp for strong
原文地址:http://www.cnblogs.com/xcwu/p/4106837.html