Directly on the topic, and slightly bored.
A + B
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 12635 Accepted Submission (s): 7407
Problem description reads two positive integers a and B less than 100, computes the a+b.
It is important to note that each digit of A and b is given by the corresponding English word.
The input test inputs contain several test cases, one row for each test case, the format "A + B =", and two adjacent strings with a space interval. When both A and B are at 0 o'clock, the corresponding result is not output.
Output outputs 1 rows for each test case, which is the value of a+b.
Sample Input
One + =three four + five six =zero seven + eight nine =zero + zero =
Sample Output
39096
SOURCE Zhejiang University Computer Postgraduate exam on the machine-2005
The main idea is to enter the two characters before the plus sign, which is the two character channeling. Detailed parsing is included with the code.
#include <stdio.h> #include <string.h>int main () {while (1)//multiple sets of data {int Sum=0,i;int X=0;char a[5000];int b[ 5000]; Use an integer array to hold the number int l=0;int d[5000];//ibid. memset (b,0,sizeof (b));//Initialize memset (d,0,sizeof (d));//Ditto while (scanf ("%s", a) &&STRCMP (A, "+")!=0)//First Enter a character channeling if equal to + jumps out {if (strcmp (A, "zero") ==0) b[l++]=0;if (strcmp (A, "one") ==0) b[l++]=1; if (strcmp (A, "B") ==0) b[l++]=2;if (strcmp (A, "three") ==0) b[l++]=3;if (strcmp (A, "four") ==0) b[l++]=4;if (strcmp (A, " Five ") ==0) b[l++]=5;if (strcmp (A," six ") ==0) b[l++]=6;if (strcmp (A," seven ") ==0) b[l++]=7;if (strcmp (A," eight ") ==0) b[l ++]=8;if (strcmp (A, "nine") ==0) b[l++]=9;} The above comparison of each character channeling is equal to the corresponding word, equal to the number of words directly into the array. for (i=0;i<l;i++) x=x*10+b[i];//Here is the key, such as the three four algorithm is 0*10+3 x=3. X=3*10+4. X=34. sum+=x;x=0;l=0;//is re-assigned Char c[5000];while (scanf ("%s", c) &&strcmp (c, "=")!=0)//Enter another character to flee, equal to the equal sign. {if (strcmp (c, "zero") ==0) D[l++]=0;if (strcmp (C, "one") ==0) D[l++]=1;if (strcmp (c, "B") ==0) D[l++]=2;if (strcmp (C, " Three ") ==0) D[l++]=3;if (strcmp (C," four ") ==0) D[l++]=4;if (strcmp (c," fiVE ") ==0) D[l++]=5;if (strcmp (C," six ") ==0) D[l++]=6;if (strcmp (C," seven ") ==0 (D[l++]=7;if (c," strcmp ") eight) ==0 ]=8;if (strcmp (C, "nine") ==0) d[l++]=9;} The for (i=0;i<l;i++) x=x*10+d[i];//method is on: The sum added to the sum+=x;//is the final answer. if (sum) printf ("%d\n", sum);//If sum is true, output. Elsereturn 0;//, or jump out. Memset (b,0,sizeof (b)); Memset (d,0,sizeof (d));//continuation of the initial insurance. }return 0;}
HDU 1228 Alternative A+b