P - A + B(第二季水)

Description

读入两个小于100的正整数A和B,计算A+B.

需要注意的是:A和B的每一位数字由对应的英文单词给出.

Input

测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.

Output

对每个测试用例输出1行,即A+B的值.

Sample Input

one + two =

three four + five six =

zero seven + eight nine =

zero + zero =

Sample Output

3 90 96

#include <iostream>
#include <string>
using namespace std;
int g(string str)
{
    if(str=="zero")return 0;
    if(str=="one")return 1;
    if(str=="two")return 2;
    if(str=="three")return 3;
    if(str=="four")return 4;
    if(str=="five")return 5;
    if(str=="six")return 6;
    if(str=="seven")return 7;
    if(str=="eight")return 8;
    if(str=="nine")return 9;
}
int main()
{
    string a[15];
    while(cin>>a[0]>>a[1]>>a[2]){
        int i,k1,k2;
        for(i=3;;i++){
            cin>>a[i];
            if(a[i]=="=")break;
        }
        if(a[0]=="zero"&&a[1]=="+"&&a[2]=="zero"&&a[3]=="=")break;
        if(a[1]=="+"){
            k1=g(a[0]);
            if(a[3]=="=")k2=g(a[2]);
            else k2=g(a[2])*10+g(a[3]);
        }
        else{
            k1=10*g(a[0])+g(a[1]);
            if(a[4]=="=")k2=g(a[3]);
            else k2=g(a[3])*10+g(a[4]);
        }
        cout<<k1+k2<<endl;
    }
    //system("pause");
    return 0;
}

依旧灵活使用字符串即可

时间: 2024-10-13 12:36:53

P - A + B(第二季水)的相关文章

F - The Fun Number System(第二季水)

Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight of the most significant bit (i.e., in position k-1), is -2^(k-1), and the weight of a bit in any position i (0 ≤ i < k-1) is 2^i. For example, a 3 bit

D - Counterfeit Dollar(第二季水)

Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coi

I - Long Distance Racing(第二季水)

Description Bessie is training for her next race by running on a path that includes hills so that she will be prepared for any terrain. She has planned a straight path and wants to run as far as she can -- but she must be back to the farm within M se

S - 骨牌铺方格(第二季水)

Description 在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.         例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:         Input 输入数据由多行组成,每行包含一个整数n,表示该测试实例的长方形方格的规格是2×n (0<n<=50). Output 对于每个测试实例,请输出铺放方案的总数,每个实例的输出占一行. Sample Input 1 3 2 Sample Output 1 3 2 水题,同上一道小蜜

V - 不容易系列之(4)――考新郎(第二季水)

Description 国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的:         首先,给每位新娘打扮得几乎一模一样,并盖上大大的红盖头随机坐成一排;         然后,让各位新郎寻找自己的新娘.每人只准找一个,并且不允许多人找一个.         最后,揭开盖头,如果找错了对象就要当众跪搓衣板...         看来做新郎也不是容易的事情...         假设一共有

W - Bitset(第二季水)

Description Give you a number on base ten,you should output it on base two.(0 < n < 1000) Input For each case there is a postive number n on base ten, end of file. Output For each case output a number on base two. Sample Input 1 2 3 Sample Output 1

J - A + B Problem II(第二季水)

Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, eac

A - 高精度(大数)N次方(第二季水)

Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.         This problem requires that yo

N - Robot Motion(第二季水)

Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are N north (up the page)         S south (down the page)