Topic 1010:a + B
time limit:1 seconds
Memory limit:32 MB
Special question: No
submitted:5913
Resolution:3075
-
Title Description:
-
Read in two positive integers a and B less than 100, calculate a+b.
It is important to note that each digit of A and b is given by the corresponding English word.
-
Input:
The
-
test input contains 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 the end of 0 o'clock input, the corresponding result is not output.
-
Output:
-
Output 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
Idea: String segmentation using Strtok function, all the number of words are hash, get the only value that can represent the number itself, here using 120 modulo, get the number of words corresponding to the numbers, to do a large number of additions, and finally get results
#include <stdio.h> #include <string.h>int m[120];int hash (char *s) {int result=1; int i=1; for (; I<strlen (s); ++i) {result+=s[i]; } return result%120;} void Init () {char *s[]={"zero", "one", "one", "three", "four", "five", "six", "seven", "Eight", "N Ine "}; for (int i=0;i<sizeof (s)/sizeof (char*); ++i) {M[hash (s[i])]=i; }}char s[100];typedef struct node{int a[101]; int b[101]; int c[101]; int size;} Node;void init2 (node *n) {for (int i=0;i<101;++i) {n->a[i]=0; n->b[i]=0; n->c[i]=0; }}int Main (int argc, char *argv[]) {init (); while (gets (s)!=null) {int A, B; Char *p=strtok (S, ""); int T=m[hash (p)]; while (P=strtok (NULL, "")) {if (strcmp (p, "+")!=0) {t*=10; T+=m[hash (p)]; } else break; } a=t; P=strtok (NULL, ""); T=m[hash (p)]; while (P=strtok (NULL, "")) {if (strcmp (p, "=")!=0) {t*=10; T+=m[hash (p)]; } else break; } b=t; if (a==0&&b==0) return 0; int i=0; Node A1; Init2 (&A1); while (a) {a1.a[i++]=a%10; a/=10; } a1.size=i; i=0; while (b) {a1.b[i++]=b%10; b/=10; } if (i>a1.size) a1.size=i; int carry=0; for (int j=0;j<a1.size;++j) {t=a1.a[j]+a1.b[j]+carry; a1.c[j]=t%10; CARRY=T/10; } if (carry!=0) {a1.c[a1.size]=carry; a1.size++; } for (int j=a1.size-1;j>=0;--j) {printf ("%d", a1.c[j]); } printf ("\ n"); } return 0;} /**********************************************************problem:1010 User:kirchhoff language:c result:accepted time:0 Ms memory:920 kb***************** ***********************************************/
Nine degrees OJ 1010 A+b