[HDU] [1228] A + B

Problem Description

Loading...

Problem Analysis

Quite an interesting problem
It is just a conversion between English strings and numbers

Here the words are stored in an array, and then the conversion is done by comparing the string with the character at the corresponding index to see which one matches

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<string.h>
char num[10][6]= {"zero","one","two","three","four","five","six","seven","eight","nine"};
int ex(char s[]) {
	for(int i=0; i<10; i++) {
		if(strcmp(s,num[i])==0)
			return i;
	}
}

int main() {
	char s[10];
	while(true) {
		int ta=0,tb=0;
		while(scanf("%s",s),strcmp(s,"+")!=0) {
			ta=ta*10+ex(s);
		}
		while(scanf("%s",s),strcmp(s,"=")!=0) {
			tb=tb*10+ex(s);
		}
		if(ta==0&&tb==0)
			break;
		else
			printf("%d\n",ta+tb);
	}
	return 0;
}

Problem link: 【HDU】[1228]A + B

Loading...