题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805279328157696

坑点

这题调试半天没过,最后发现是因为num初始化在循环外面。。。真是醉了。

代码

  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <cstring>
  5. using namespace std;
  6. string list1[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
  7. string list2[13] = {"error","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
  8. map<string, int> str_to_num;
  9. string num_to_str[170];
  10. void init(){
  11. //初始化num_to_str
  12. for(int i = 0; i < 13; i++){
  13. num_to_str[i] = list1[i];
  14. }
  15. for(int i = 13; i < 169; i++){
  16. if(i % 13 == 0)
  17. num_to_str[i] = list2[i/13];
  18. else num_to_str[i] = list2[i/13] + " " + list1[i%13];
  19. }
  20. //初始化str_to_num
  21. for(int i = 0; i < 169; i++){
  22. str_to_num[num_to_str[i]] = i;
  23. }
  24. }
  25. int main() {
  26. init();
  27. int n;
  28. string tempstr;
  29. scanf("%d",&n);
  30. getchar();
  31. for(int i = 0; i < n; i++){
  32. int num = 0;
  33. getline(cin, tempstr);
  34. if(tempstr[0]<='9' && tempstr[0] >= '0'){
  35. for(int j = 0; j < tempstr.size(); j++){
  36. num = num * 10 + tempstr[j] - '0';
  37. }
  38. cout<<num_to_str[num]<<endl;
  39. } else {
  40. cout<<str_to_num[tempstr]<<endl;
  41. }
  42. }
  43. return 0;
  44. }