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

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

浙大计算机研究生复试上机考试-2005年

解题思路:

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

代码:

方法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;
}

[ACM] hdu 1228 A+B (字符串处理),布布扣,bubuko.com

时间: 2024-08-27 00:34:02

[ACM] hdu 1228 A+B (字符串处理)的相关文章

hdu 1228 A + B 详细题解 字符串/哈希

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1228 这道题可以同时用两种方法做,第一种是字符串,第二种是哈希. 第一种方法: 我们可以定义一个字符串类型的二位数组,存放"zero"--"nine"十个字符串 这十个字符串可以与下标0--9一一对应.这样就可以建立字符串与数字之间的关系了 char a[][10]={"zero","one","two",&q

题解报告:hdu 1228 A+B

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1228 题解思路:这道题本来想用gets函数,循环读进来的字符串,把每一个单词存到二维数组里面,再与已存在的二维数组进行比较.但看了别人的题解后才发现忘了scanf有这个功能即遇到空格.换行.回车.水平制表符.换页符.垂直制表符就会停止读取(这里用到的是空格的功能),不仅效率高,而且思路清晰,代码简洁. AC代码: 1 #include<bits/stdc++.h> 2 using namespac

[ACM] hdu 1251 统计难题 (字典树)

统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每一个提问都是一个字符串. 注意:本题仅仅有一组測试数据,处理到文件结束. Out

HDU 1228 A + B 的浙大考研题

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 + eig

HDU-4632 http://acm.hdu.edu.cn/showproblem.php?pid=4632

http://acm.hdu.edu.cn/showproblem.php?pid=4632 题意: 一个字符串,有多少个subsequence是回文串. 别人的题解: 用dp[i][j]表示这一段里有多少个回文串,那首先dp[i][j]=dp[i+1][j]+dp[i][j-1],但是dp[i+1][j]和dp[i][j-1]可能有公共部分,所以要减去dp[i+1][j-1]. 如果str[i]==str[j]的话,还要加上dp[i+1][j-1]+1. 但是自己却是这样想的,把每个区间都要看

[ACM] hdu 1217 Arbitrage (bellman_ford最短路,判断是否有正权回路或Floyed)

Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British p

【HDU 5510 Bazinga】字符串

2015沈阳区域赛现场赛第2题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:给定一个由字符串组成的序列,一共n个元素,每个元素是一个不超过2000个字符的字符串.求"存在秩小于 i 且不是 i 的子串"的最大的 i (1<= i <= n). 数据范围:n [1, 500],T组输入,T [1, 50] 思路:从1到n-1枚举每个字符串str[i],判断是否有 j < i 使得str[j]不是str[i

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

Sdut2411 Pixel density 山东省第三届ACM省赛(输入输出字符串处理)

本文出处:http://blog.csdn.net/svitter 原题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2411 题意:给你一个串,让你依据那个串来输出ppi.坑特别多.ppi的计算方法是dp / inches; dp = sqrt(wp*wp + hp * hp); 现在我来说说这个题目有多坑: 给你的串的格式是这样: name + inches+ "inches"