zoukankan      html  css  js  c++  java
  • A + B

    Description

    读入两个小于10000的正整数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
    

    HINT

    //自己的方法并不是特别简单但是易懂
    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	int i,j;
    	int x,y,m,n,t; 
    	char a[1000],b[1000],c[100][100],d[100][100],str[10000];
    	while(gets(str))
    	{ 
    		t=0;
    		for(i=0;str[i];i++)
    		{
    			if(str[i]=='+')
    			{
    				a[t-1]='';//因为“+”前有一个空格
    				break;
    			}
    			else
    				a[t++]=str[i];
    		}
    		a[t]='';
    		t=0;
    		for(j=i+2;str[j];j++)//(j=i+2)因为“+”后有一个空格
    		{
    			if(str[j]=='=')
    			{
    				b[t-1]='';//因为“=”前有一个空格
    				break;
    			}
    			else
    				b[t++]=str[j];
    		}
    		b[t]='';
    		
    		if(strcmp(a,"zero")==0&&strcmp(b,"zero")==0)
    			break;
    		//printf("%s
    ",a);
    	//	printf("%s
    ",b);
    		m=0;
    		n=0;
    		for(i=0;a[i];i++)
    		{
    			if(a[i]==' ')
    			{
    				d[m][n++]='';
    				m++;
    				n=0;
    			}
    			else
    				d[m][n++]=a[i];
    		}
    		d[m++][n]='';
    		x=0;
    		for(i=0;i<m;i++)
    		{
    			if(strcmp(d[i],"one")==0)
    			{
    				j=1;
    				x=x*10+j;
    			}
    			else if(strcmp(d[i],"two")==0)
    			{
    				j=2;
    				x=x*10+j;
    			}
    			else if(strcmp(d[i],"three")==0)
    			{
    				j=3;
    				x=x*10+j;
    			}
    			else if(strcmp(d[i],"four")==0)
    			{
    				j=4;
    				x=x*10+j;
    			}
    			else if(strcmp(d[i],"five")==0)
    			{
    				j=5;
    				x=x*10+j;
    			}
    			else if(strcmp(d[i],"six")==0)
    			{
    				j=6;
    				x=x*10+j;
    			}
    			else if(strcmp(d[i],"seven")==0)
    			{
    				j=7;
    				x=x*10+j;
    			}
    			else if(strcmp(d[i],"eight")==0)
    			{
    				j=8;
    				x=x*10+j;
    			}
    			else if(strcmp(d[i],"nine")==0)
    			{
    				j=9;
    				x=x*10+j;
    			}
    			else if(strcmp(d[i],"zero")==0)
    			{
    				j=0;
    				x=x*10+j;
    			}
    		}
    		m=0;
    		n=0;
    		
    		for(i=0;b[i];i++)
    		{
    			if(b[i]==' ')
    			{
    				c[m][n++]='';
    				m++;
    				n=0;
    			}
    			else
    				c[m][n++]=b[i];
    		}
    		c[m++][n]='';
    		y=0;
    		for(i=0;i<m;i++)
    		{
    			if(strcmp(c[i],"one")==0)
    			{
    				j=1;
    				y=y*10+j;
    			}
    			else if(strcmp(c[i],"two")==0)
    			{
    				j=2;
    				y=y*10+j;
    			}
    			else if(strcmp(c[i],"three")==0)
    			{
    				j=3;
    				y=y*10+j;
    			}
    			else if(strcmp(c[i],"four")==0)
    			{
    				j=4;
    				y=y*10+j;
    			}
    			else if(strcmp(c[i],"five")==0)
    			{
    				j=5;
    				y=y*10+j;
    			}
    			else if(strcmp(c[i],"six")==0)
    			{
    				j=6;
    				y=y*10+j;
    			}
    			else if(strcmp(c[i],"seven")==0)
    			{
    				j=7;
    				y=y*10+j;
    			}
    			else if(strcmp(c[i],"eight")==0)
    			{
    				j=8;
    				y=y*10+j;
    			}
    			else if(strcmp(c[i],"nine")==0)
    			{
    				j=9;
    				y=y*10+j;
    			}
    			else if(strcmp(c[i],"zero")==0)
    			{
    				j=0;
    				y=y*10+j;
    			}
    		}
    		printf("%d
    ",x+y);
    	}
    	return 0;
    }
    //学习其他的方法
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <stdio.h>
    using namespace std;
     
    int digital(string str)  
    {
        if(str == "one")
            return 1 ;
        else if(str == "two")
            return 2 ;
        else if(str == "three")
            return 3 ;
        else if(str == "four")
            return 4 ;
        else if(str == "five")
            return 5 ;
        else if(str == "six")
            return 6 ;
        else if(str == "seven")
            return 7 ;
        else if(str == "eight")
            return 8 ;
        else if(str == "nine")
            return 9 ;
        return 0 ;
    }
     
    int main()
    {
        char s[1000] ;
        while(gets(s))  
    	{
            string str[100] ;//字符串数组
            char *p = NULL ;//p初始值定为空
            int count = 0 ;
            for(p = strtok(s," ") ; p ; p = strtok(NULL," "))//p = strtok(s," ")将其按碰到第一个空格时拆分,p = strtok(NULL," ")将剩余的后面一部分(NULL)按空格分,直到p为空时
                str[count++] = p ;
            int a = 0 , b = 0 ;
            int i = 0 ;
            for( ; str[i] != "+" ; i++) //“+”之前为A
    		{
                int di = digital(str[i]) ;
                a = a * 10 ;
                a = a + di ;
            }
            for( ; str[i] != "=" ; i++)//"+"之后“=”之前为B
    		{
                int di = digital(str[i]) ;
                b = b * 10 ;
                b = b + di ;
            }
            if(a+b == 0)
                return 0 ;
            cout << a + b << endl ;
        }
        return 0;
    }


  • 相关阅读:
    【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
    Educational Codeforces Round 73 (Rated for Div. 2)F(线段树,扫描线)
    【PAT甲级】1042 Shuffling Machine (20 分)
    【PAT甲级】1041 Be Unique (20 分)(多重集)
    【PAT甲级】1040 Longest Symmetric String (25 分)(cin.getline(s,1007))
    【PAT甲级】1039 Course List for Student (25 分)(vector嵌套于map,段错误原因未知)
    Codeforces Round #588 (Div. 2)E(DFS,思维,__gcd,树)
    2017-3-9 SQL server 数据库
    2017-3-8 学生信息展示习题
    2017-3-5 C#基础 函数--递归
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4237329.html
Copyright © 2011-2022 走看看