1044 火星数字 (20 分)

小灰灰 2021-10-23 07:09 414阅读 0赞

火星人是以 13 进制计数的:

  • 地球人的 0 被火星人称为 tret。
  • 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。
  • 火星人将进位以后的 12 个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。

例如地球人的数字 29 翻译成火星文就是 hel mar;而火星文 elo nov 对应地球数字 115。为了方便交流,请你编写程序实现地球和火星数字之间的互译。

输入格式:

输入第一行给出一个正整数 N(<),随后 N 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。

输出格式:

对应输入的每一行,在一行中输出翻译后的另一种语言的数字。

输入样例:

  1. 4
  2. 29
  3. 5
  4. elo nov
  5. tam

输出样例:

  1. hel mar
  2. may
  3. 115
  4. 13

这题瞎写完写注释的时候发现自己想错了,居然歪打正着,问题不大

  1. #include<iostream>
  2. #include <cmath>
  3. #include <string>
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. string s1[13]={
  7. "tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
  8. string s2[13]={
  9. "","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
  10. int tonum(string str){
  11. int num=0;
  12. int n=str.length();
  13. for(int i=0;i<n;i++){
  14. num=num*10+str[i]-'0';
  15. }
  16. return num;
  17. }
  18. void to13(string str){
  19. //转火星文
  20. int x=tonum(str);//string转int
  21. int m=x;
  22. stack<char> s;//10进制转13进制输出为string类型到str1
  23. while(x){
  24. s.push('0'+x%13);
  25. x=x/13;
  26. }
  27. string str1;
  28. if(m==0){
  29. //排除输入0的异常情况
  30. str1=str1+'0';
  31. }
  32. while(!s.empty()){
  33. str1=str1+s.top();
  34. s.pop();
  35. }
  36. if(str1.length()<2){
  37. //若是一位数
  38. cout<<s1[str1[0]-'0'];
  39. }else{
  40. if(str1[1]=='0'){
  41. cout<<s2[str1[0]-'0'];
  42. }else{
  43. cout<<s2[str1[0]-'0']<<" ";
  44. cout<<s1[str1[1]-'0'];
  45. }
  46. }
  47. }
  48. void to10(string str){
  49. if(str.length()<5){
  50. int i;
  51. for(i=0;i<55;i++){
  52. if(str==s1[i]){
  53. cout<<i;
  54. break;
  55. }else if(str==s2[i]){
  56. cout<<i*13;
  57. break;
  58. }
  59. }
  60. }else{
  61. string ss(str,0,3);
  62. //cout<<ss;
  63. int num=0;
  64. int i;
  65. for(i=0;i<55;i++){
  66. //cout<<i;
  67. if(ss==s2[i]){
  68. num=i*13;
  69. break;
  70. }
  71. }
  72. string sss(str,4,6);
  73. // cout<<sss;
  74. for(i=0;i<55;i++){
  75. if(sss==s1[i]){
  76. num=num+i;
  77. break;
  78. }
  79. }
  80. cout<<num;
  81. }
  82. }
  83. int main()
  84. {
  85. int N,i,j;
  86. cin>>N;
  87. getchar();
  88. for(i=0;i<N;i++){
  89. string str;
  90. int sum=0;
  91. getline(cin,str);
  92. if(str[0]>='0'&&str[0]<='9'){
  93. to13(str);
  94. cout<<endl;
  95. }else{
  96. to10(str);
  97. cout<<endl;
  98. }
  99. }
  100. return 0;
  101. }

转载于:https://www.cnblogs.com/siro/p/11206457.html

发表评论

表情:
评论列表 (有 0 条评论,414人围观)

还没有评论,来说两句吧...

相关阅读