UVa 486-english-number Translator

Source: Internet
Author: User

Title: give you a number in English, translated into Arabic numerals (no and).

Analysis: Simulation, recursion. This can be solved in many ways.

Here, using recursion, divide the number into parts and (only million, thousand, million), respectively, to solve the sum.

Description: (⊙_⊙).

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace Std;char buf[20][10];char number[32][10] = {"Negative", "zero", "one", " "Three", "four", "five", "six", "seven", "eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "fourteen", "fifteen "," Sixteen "," Seventeen "," Eighteen "," Nineteen "," Twenty "," Thirty "," Forty "," Fifty "," Sixty "," Seventy "," Eighty "," ni Nety "," Hundred "," thousand "," Million "}; int value[32] = { -1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90,100,1000,1000000}; int find (char *str) {for (int i = 1; i < + + i) if (!strcmp (str, number[i]) return value[i];return-1;} int dfs (int a, int b) {if (a > B) return 0;if (A = = b) return to find (Buf[a]); else {int million = 0,thousand = 0,hundred =  0,sum = 0;for (int i = A; I <= B; + + i) {if (!strcmp (Buf[i], "million")) million = I;if (!strcmp (buf[i), "thousand")) thousand = i;if(!STRCMP (Buf[i], "hundred")) hundred = i;sum + = find (Buf[i]);} if (million) return Dfs (A, million-1) *1000000+dfs (million+1, B), if (Thousand) return Dfs (A, thousand-1) *1000+dfs ( Thousand+1, b); if (hundred) return Dfs (A, hundred-1) *100+dfs (hundred+1, b); return sum;}} int main () {int count = 0;while (scanf ("%s", Buf[count + +])! = EOF) {while (GetChar ()! = ' \ n ') scanf ("%s", Buf[count + +]); if (    !STRCMP (buf[0], "negative")) printf ("-%d\n", DFS (1, count-1)), Else printf ("%d\n", DFS (0, count-1)); count = 0;} return 0;}


UVa 486-english-number Translator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.