zl程序教程

您现在的位置是:首页 >  其它

当前栏目

[ACM] hdu 1228 A+B (字符串处理)

处理 字符串 HDU ACM
2023-09-27 14:25:12 时间

A + B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11543    Accepted Submission(s): 6699


Problem 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
 


 

Source

 

 

解题思路:

以加号为界限,左右两个加数分别存到一个字符串里面,再在每一个字符串中提取出来加数。

代码:

方法1:使用substr函数,手动推断空格

#include <iostream>
#include <string.h>
using namespace std;

int change(string str)//字符串转换成数字
{
    int d;
    if(str=="zero")
        d=0;
    else if(str=="one")
        d=1;
    else if(str=="two")
        d=2;
    else if(str=="three")
        d=3;
    else if(str=="four")
        d=4;
    else if(str=="five")
        d=5;
    else if(str=="six")
        d=6;
    else if(str=="seven")
        d=7;
    else if(str=="eight")
        d=8;
    else if(str=="nine")
        d=9;
    return d;
}
int main()
{
    string exp;//输入的一行
    string A,B;int a,b;//A,B分别代表加号左,右的数的字符串,a,b分别为两个加数的值
    while(getline(cin,exp))
    {
        int len=exp.length();
        int j;
        int tap1,tap2;
        for(j=0;j<len;j++)
          {
              if(exp[j]==' '&&exp[j+1]=='+')
                tap1=j;//tap1为第一个数右边的空格
              if(exp[j]==' '&&exp[j+1]=='=')
                tap2=j;//tap2为第二个数右边的空格
          }
        A=exp.substr(0,tap1);//提取,開始位置为0,提取长度为tap1
        B=exp.substr(tap1+3,tap2-tap1-3);
        int lenA=A.length();
        int lenB=B.length();
        a=b=0;
        int pre=-1;
        for(int i=0;i<lenA;i++)
        {
            if(A[i]==' ')
            {
                  a=a*10+change(A.substr(pre+1,i-pre-1));
                  pre=i;
            }
            if(i==lenA-1)//和空格的情况不太一样,要多读取一位
                 a=a*10+change(A.substr(pre+1,i-pre));
        }
        pre=-1;
        for(int i=0;i<lenB;i++)
        {
            if(B[i]==' ')
            {
                  b=b*10+change(B.substr(pre+1,i-pre-1));
                  pre=i;
            }
            if(i==lenB-1)
                 b=b*10+change(B.substr(pre+1,i-pre));
        }
        if(a==0&&b==0)
            break;
        cout<<a+b<<endl;
    }
    return 0;
}

方法2,3:(输入时,自己主动忽略空格,把每一个单词放入到一个字符数组中)

代码1:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char s[100][100];

int change(char str[])
{
    int d;
    if(str[0]=='z')//不能str=="zero"
        d=0;
    else if(str[0]=='o')
        d=1;
    else if(str[0]=='t'&&str[1]=='w')
        d=2;
    else if(str[0]=='t'&&str[1]=='h')
        d=3;
    else if(str[0]=='f'&&str[1]=='o')
        d=4;
    else if(str[0]=='f'&&str[1]=='i')
        d=5;
    else if(str[0]=='s'&&str[1]=='i')
        d=6;
    else if(str[0]=='s'&&str[1]=='e')
        d=7;
    else if(str[0]=='e')
        d=8;
    else if(str[0]=='n')
        d=9;
    return d;
}
int main()
{
    int a,b;
    int c = 0;
    while(~scanf("%s", s[c])){//先输入第一个单词
        c = 1;
        char ch;
        while(scanf("%s%c",s[c], &ch)){//以空格为界限。读入每一个单词,字符数组不读空格
            if(ch == '\n')//退出条件
                break;
            c++;
        }
        int ok=0;
        a=b=0;
        for(int i=0;i<c;i++)
        {
            if(s[i][0]=='+')
            {
                ok=1;
                continue;
            }
            if(ok==0)
                a=a*10+change(s[i]);
            else if(ok==1)
                b=b*10+change(s[i]);
        }
        if(a==0&&b==0)
            break;
        cout<<a+b<<endl;
        c = 0;
    }
    return 0;
}


代码2:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char s[100][100];

int change(char str[])
{
    int d;
    if(str[0]=='z')//不能str=="zero"
        d=0;
    else if(str[0]=='o')
        d=1;
    else if(str[0]=='t'&&str[1]=='w')
        d=2;
    else if(str[0]=='t'&&str[1]=='h')
        d=3;
    else if(str[0]=='f'&&str[1]=='o')
        d=4;
    else if(str[0]=='f'&&str[1]=='i')
        d=5;
    else if(str[0]=='s'&&str[1]=='i')
        d=6;
    else if(str[0]=='s'&&str[1]=='e')
        d=7;
    else if(str[0]=='e')
        d=8;
    else if(str[0]=='n')
        d=9;
    return d;
}
int main()
{
    while(1)
    {
        char ch;
        int a,b;
        int c=0;
        while(scanf("%s%c",s[c],&ch))//输入每一个单词
        {
            if(ch=='\n')
                break;
            c++;
        }
        int ok=0;
        a=b=0;
        for(int i=0;i<c;i++)//s[c]里面在该题存的是”=“,没用
        {
            if(s[i][0]=='+')
            {
                ok=1;
                continue;
            }
            if(ok==0)
                a=a*10+change(s[i]);
            else if(ok==1)
                b=b*10+change(s[i]);
        }
        if(a==0&&b==0)
            break;
        cout<<a+b<<endl;

    }
    return 0;
}