1 条题解
-
0
Description
In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. The numbers can range from negative 999,999,999 to positive 999,999,999. The following is an exhaustive list of English words that your program must account for:
negative, zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand, million
Input
The input consists of several instances. Notes on input:
Negative numbers will be preceded by the word negative.
<li>The word "hundred" is not used when "thousand" could be. For example, 1500 is written "one thousand five hundred", not "fifteen hundred". </li>The input is terminated by an empty line.
Output
The answers are expected to be on separate lines with a newline after each.
输入数据 1
six
negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two
输出数据 1
6
-729
1000101
814022
Source
CTU Open 2004,UVA 486
题目描述
在这个问题中,你需要将一个或多个用英文单词表示的数字转换为对应的整数形式。数字的范围是从-999,999,999到999,999,999。以下是程序需要处理的英文单词的完整列表:
negative, zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand, million
输入说明
输入包含多个测试用例。注意以下几点:
负数前面会有单词"negative"。
当可以使用"thousand"时,不会使用"hundred"。例如,1500写作"one thousand five hundred",而不是"fifteen hundred"。
输入以空行结束。
输出说明
每个测试用例的输出应该独占一行,每个数字后面有一个换行符。
示例输入 1
six
negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two
示例输出 1
6
-729
1000101
814022
题目来源
CTU Open 2004, UVA 486
算法标签:
字符转换,数字组合;
题意分析
这个问题要求我们将英文数字转换为整数形式,注意单位和数字的组合。
解题思路
单词到数字的映射:建立一个字典将英文单词映射到对应的数字或单位。
负数处理:如果数字前有"negative",则最终结果为负数。
单位处理:正确处理"hundred"、"thousand"和"million"等单位,这些单位会影响数字的位数。
组合数字:需要将各个部分的数字正确地组合起来,考虑单位的影响。
标程
#include <iostream> #include <sstream> #include <string> #include <map> #include <stdexcept> using namespace std; class NumberConverter { private: map<string, long long> wordToValue; public: NumberConverter() { wordToValue["negative"] = -1; wordToValue["zero"] = 0; wordToValue["one"] = 1; wordToValue["two"] = 2; wordToValue["three"] = 3; wordToValue["four"] = 4; wordToValue["five"] = 5; wordToValue["six"] = 6; wordToValue["seven"] = 7; wordToValue["eight"] = 8; wordToValue["nine"] = 9; wordToValue["ten"] = 10; wordToValue["eleven"] = 11; wordToValue["twelve"] = 12; wordToValue["thirteen"] = 13; wordToValue["fourteen"] = 14; wordToValue["fifteen"] = 15; wordToValue["sixteen"] = 16; wordToValue["seventeen"] = 17; wordToValue["eighteen"] = 18; wordToValue["nineteen"] = 19; wordToValue["twenty"] = 20; wordToValue["thirty"] = 30; wordToValue["forty"] = 40; wordToValue["fifty"] = 50; wordToValue["sixty"] = 60; wordToValue["seventy"] = 70; wordToValue["eighty"] = 80; wordToValue["ninety"] = 90; wordToValue["hundred"] = 100; wordToValue["thousand"] = 1000; wordToValue["million"] = 1000000; } long long convert(const string& line) { if (line.empty()) throw invalid_argument("输入不能为空"); istringstream iss(line); string word; long long current = 0; long long total = 0; bool isNegative = false; while (iss >> word) { map<string, long long>::iterator it = wordToValue.find(word); if (it == wordToValue.end()) { throw invalid_argument("未知的数字单词: " + word); } long long value = it->second; if (word == "negative") { isNegative = true; } else if (value == 100) { current *= value; } else if (value >= 1000) { total += current * value; current = 0; } else { current += value; } } total += current; return isNegative ? -total : total; } }; int main() { NumberConverter converter; string line; while (getline(cin, line)) { if (line.empty()) break; try { cout << converter.convert(line) << endl; } catch (const exception& e) { cerr << "错误: " << e.what() << endl; } } return 0; }
- 1
信息
- ID
- 1122
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者