HDU's ACM 1228 A + B

原題鏈接:HDU’s ACM 1228 A + B


分析:此爲簡單的字符串處理的問題,可以藉助庫函數處理:strcmp


AC Code:

<span style="font-family:Microsoft YaHei;font-size:14px;">#include <stdio.h>
#include <string.h>

const char numStr[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

int main()
{
	int num1, num2;
	char tmp[10];
	int i;
	while(1) {
		tmp[0] = '\0';
		num1 = 0;
		do {
			scanf("%s", tmp);

			for(i=0;i<10;++i){
				if(strcmp(tmp, numStr[i])==0){
					num1 = num1*10 + i;
				}
			}
		} while(strcmp(tmp, "+") != 0);
		num2 = 0;
		do {
			scanf("%s", tmp);

			for(i=0;i<10;++i){
				if(strcmp(tmp, numStr[i])==0){
					num2 = num2*10 + i;
				}
			}
		} while(strcmp(tmp, "=") != 0);

		if(!num1 && !num2)
			break;

		printf("%d\n", num1+num2);
	}
	return 0;
}</span>


点赞