题解 | A + B

A + B

https://www.nowcoder.com/practice/5fb3548802bb4a13a10c2e53a6fbfdd9

#include <stdio.h>
#include <string.h>

int word_to_num(char *s) {
    if (strcmp(s, "zero") == 0) return 0;
    if (strcmp(s, "one") == 0) return 1;
    if (strcmp(s, "two") == 0) return 2;
    if (strcmp(s, "three") == 0) return 3;
    if (strcmp(s, "four") == 0) return 4;
    if (strcmp(s, "five") == 0) return 5;
    if (strcmp(s, "six") == 0) return 6;
    if (strcmp(s, "seven") == 0) return 7;
    if (strcmp(s, "eight") == 0) return 8;
    if (strcmp(s, "nine") == 0) return 9;
    return 0;
}

int main() {
    char word[20];
    int current_num = 0; 
    int a = 0, b = 0;      // 不需要初始化为 -1,因为一定会被赋值

while(scanf("%s",word)!=EOF){
    if(strcmp(word,"+")==0){
        a=current_num;
        current_num=0;
    }else if(strcmp(word,"=")==0){
        b=current_num;
        if(a==0&&b==0) break;
          else {
            printf("%d\n",a+b);
            a=0;
            b=0;
            current_num=0;
          }
    }else {
        current_num=current_num*10+word_to_num(word);
    }
}
    return 0;
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务