hdu1228A+B

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1228


水..


#include <cstdio>
#include <cstring>

char s[100];
char s1[100];

void cmp(char s1[],int &a)

{
    if(strcmp(s1,"zero") == 0)
        a = a * 10 + 0;
    else if(strcmp(s1,"one") == 0)
        a = a * 10 + 1;
    else if(strcmp(s1,"two") == 0)
        a = a * 10 + 2;
    else if(strcmp(s1,"three") == 0)
        a = a * 10 + 3;
    else if(strcmp(s1,"four") == 0)
        a = a * 10 + 4;
    else if(strcmp(s1,"five") == 0)
        a = a * 10 + 5;
    else if(strcmp(s1,"six") == 0)
        a = a * 10 + 6;
    else if(strcmp(s1,"seven") == 0)
        a = a * 10 + 7;
    else if(strcmp(s1,"eight") == 0)
        a = a * 10 + 8;
    else if(strcmp(s1,"nine") == 0)
        a = a * 10 + 9;
}
void solve(char s[],int& a,int& b)

{
    int i,j;
    int k = 0;
    int len = strlen(s);
    for(i = 0; s[i] != '+'; ++i)
    {
        if(s[i] == ' ')
        {
            s1[k] = '\0';
            cmp(s1,a);
            k = 0;
        }
        else
        {
            s1[k++] = s[i];
        }
    }
    for(j = i + 2; s[j] != '='; ++j)
    {
        if(s[j] == ' ')
        {
            s1[k] = '\0';
            cmp(s1,b);
            k = 0;
        }
        else
            s1[k++] = s[j];
    }
}

int main()

{
    while(gets(s))
    {
        int a,b;
        a = 0;
        b = 0;
        solve(s,a,b);
        if(!a && !b)
            break;
        printf("%d\n",a + b);
    }
    return 0;
}

你可能感兴趣的:(字符串,ACM)