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