题解:UVA486 English-Number Translator
题目传送门
题目简述
给定一个去除 and 和 - 并且 negative。
主要思路
对于负数,只需要记录一个变量 negative 记为
设当前累积的数字为
thousand与million:由于数字的绝对值<10^{9} ,所以这两种单词后一个单词(如果有)只能\le90 。将now\_num 乘上1000 或1000000 ,ans \to ans + now\_num ,now\_num \to 0 。-
hundred:hundred后面的单词既可以\le90 也可以是thousand或million,但这两种情况都只需要now\_num \to now\_num \times 100 。因为对于第一种情况,hundred后一个不是thousand和million根据英语语法再后面所有单词只能\le 90 ,在这种情况下,now_num不清零直接根据\le90 的数的处理方式也不影响结果。
处理完每个单词后,还需让
AC Code
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define OUT 0
#define MAMBA return
typedef long long ll;
typedef long double db;
const int INT_INF = 0x3f3f3f3f;
int man();int main(){MAMBA man();}
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
inline int _abs(int a) { if (a < 0) return -a; return a; }
inline int _pow(int a, int b) { int x = 1, y = a; while(b > 0) {if (b & 1) x *= y; y *= y; b >>= 1; } return x; }
// ----------------------------
// ----------------------------
vector<string> vec;
// ----------------------------
int calc(string s) {
if (s == "one") return 1;
if (s == "two") return 2;
if (s == "three") return 3;
if (s == "four") return 4;
if (s == "five") return 5;
if (s == "six") return 6;
if (s == "seven") return 7;
if (s == "eight") return 8;
if (s == "nine") return 9;
if (s == "ten") return 10;
if (s == "eleven") return 11;
if (s == "twelve") return 12;
if (s == "thirteen") return 13;
if (s == "fourteen") return 14;
if (s == "fifteen") return 15;
if (s == "sixteen") return 16;
if (s == "seventeen") return 17;
if (s == "eighteen") return 18;
if (s == "nineteen") return 19;
if (s == "twenty") return 20;
if (s == "thirty") return 30;
if (s == "forty") return 40;
if (s == "fifty") return 50;
if (s == "sixty") return 60;
if (s == "seventy") return 70;
if (s == "eighty") return 80;
if (s == "ninety") return 90;
if (s == "hundred") return 100;
if (s == "thousand") return 1000;
if (s == "million") return 1000000;
return 0;
}
int man() {
string s;
int idx, last_idx, f, ans, now_num;
while (getline(cin, s)) {
vec.clear();
last_idx = 0;
idx = s.find(" ");
while (idx != s.npos) {
vec.push_back(s.substr(last_idx, idx - last_idx));
last_idx = idx + 1;
idx = s.find(" ", last_idx);
}
vec.push_back(s.substr(last_idx)); // 以上为分隔单词
f = 1;
ans = now_num = 0;
if (vec[0] == "negative") {
f = -1;
vec.erase(vec.begin());
} // 以上为判断负数
for (int i = 0; i < (int)vec.size(); i++) {
if (vec[i] == "thousand" || vec[i] == "million") {
now_num *= calc(vec[i]);
ans += now_num;
now_num = 0;
}
else if (calc(vec[i]) <= 90) now_num += calc(vec[i]);
else if (vec[i] == "hundred") now_num *= 100;
}
ans += now_num;
cout << ans * f << endl;
}
MAMBA OUT;
}
/*
.-~~~~~~~~~-._ _.-~~~~~~~~~-.
__.' ~. .~ `.__
.'// A C 之 \./ 之 真 理 \`.
.'// | \`.
.'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \`.
.'//.-" `-. | .-' "-.\`.
.'//______.============-.. \ | / ..-============.______\`.
.'______________________________\|/______________________________`.
*/