本文主要是介绍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("1\n2\n3\n4\n5\n6\n7\n");int sum = 0;for (std::string line; std::getline(input, line); ) {sum += std::stoi(line);}std::cout << "\nThe sum is: " << sum << "\n\n";// 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同时使用时的注意事项https://blog.csdn.net/m0_37536854/article/details/828733213.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;
}
这篇关于PAT乙级刷题/1044 火星数字/C++实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!