【杭电】[1228]A + B

这里写图片描述
这里写图片描述

挺有意思的一道题
就是一个英语字符串与数字的转化

这里把单词储存为一个数组,然后通过比较字符串与对应哪个下标字符相同,进而来进行转换

#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;
}

题目地址:【杭电】[1228]A + B

posted @ 2016-03-01 22:56  BoilTask  阅读(5)  评论(0编辑  收藏  举报