PAT乙级刷题/1044 火星数字/C++实现

阅读: 评论:0

PAT乙级刷题/1044 火星数字/C++实现

PAT乙级刷题/1044 火星数字/C++实现

一、题目描述

火星人是以 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(<100),随后 N 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。

输出格式:

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

输入样例:

4
29
5
elo nov
tam
//结尾无空行

输出样例:

hel mar
may
115
13
//结尾无空行

二、思路分析

1.  getline() 的用法

#include <string>
#include <iostream>
#include <sstream>int main()
{// greet the userstd::string name;std::cout << "What is your name? ";std::getline(std::cin, name);std::cout << "Hello " << name << ", nice to meet you.n";// read file line by linestd::istringstream input;input.str("1n2n3n4n5n6n7n");int sum = 0;for (std::string line; std::getline(input, line); ) {sum += std::stoi(line);}std::cout << "nThe sum is: " << sum << "nn";// use separator to read parts of the linestd::istringstream input2;input2.str("a;b;c;d");for (std::string line; std::getline(input2, line, ';'); ) {std::cout << line << 'n';}
}
What is your name? John Q. Public
Hello John Q. Public, nice to meet you.The sum is 28a
b
c
d

 2.cin和getline()一起使用的注意事项

C++中getline()和cin同时使用时的注意事项.stoi的用法

#include <iostream>
#include <string>int main()
{std::string str1 = "45";std::string str2 = "3.14159";std::string str3 = "31337 with words";std::string str4 = "words and 2";int myint1 = std::stoi(str1);int myint2 = std::stoi(str2);int myint3 = std::stoi(str3);// error: 'std::invalid_argument'// int myint4 = std::stoi(str4);std::cout << "std::stoi("" << str1 << "") is " << myint1 << 'n';std::cout << "std::stoi("" << str2 << "") is " << myint2 << 'n';std::cout << "std::stoi("" << str3 << "") is " << myint3 << 'n';//std::cout << "std::stoi("" << str4 << "") is " << myint4 << 'n';
}
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337

 三、题解代码以及提交截图

#include <iostream>
#include <string>
using namespace std;const string low_digit[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
const string high_digit[13] = {"#", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};void MarToEarth(const string& s){int t1 = 0, t2 = 0,len = s.length();string s1 = s.substr(0, 3), s2;if (len > 4) s2 = s.substr(4, 3);for (int j = 1; j <= 12; j++) {if (s1 == low_digit[j] || s2 == low_digit[j]) t2 = j;if (s1 == high_digit[j]) t1 = j;}cout << t1 * 13 + t2;
}void EarthToMar(const int& t){if (t / 13) cout << high_digit[t / 13];if ((t / 13) && (t % 13)) cout << " ";if (t % 13 || t == 0) cout << low_digit[t % 13];
}int main()
{int N;string s;cin >> N;getchar();for (int i = 0; i < N; i++) {getline(cin, s);if (s[0] >= '0' && s[0] <= '9')EarthToMar(stoi(s));elseMarToEarth(s);cout << endl;}return 0;
}

 

 

本文发布于:2024-01-31 18:24:32,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170669667230474.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:火星   乙级   数字   PAT
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23