题解:UVA486 English-Number Translator

· · 题解

题目传送门

题目简述

给定一个去除 and- 并且 <10^{9},>-10^{9} 的数的英语单词,将这个数转为阿拉伯数字。负数在这个单词前面添上 negative

主要思路

对于负数,只需要记录一个变量 f,没有 negative 记为 1,反之记为 -1,答案只记录这个数的绝对值,输出答案时将答案乘上 f

设当前累积的数字为 now\_num,答案为 ans,初值都为 0。那么依次处理每个单词,对于每种单词有不同的处理方式:

处理完每个单词后,还需让 ans \to ans + now\_num,因为假如最后以 \le90 的数结尾,并不会让 ans 增加。

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    之   \./  之    真    理  \`.
         .'//                     |                     \`.
       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \`.
     .'//.-"                 `-.  |  .-'                 "-.\`.
   .'//______.============-..   \ | /   ..-============.______\`.
 .'______________________________\|/______________________________`.
*/