首页 > 代码库 > 题目1010:A + B(字符串拆分)

题目1010:A + B(字符串拆分)

题目链接:http://ac.jobdu.com/problem.php?pid=1010

详解连接:https://github.com/Pacsiy/JobDu

参考代码:

//// Created by AlvinZH on 2017/4/30.// Copyright (c) AlvinZH. All rights reserved.//#include <iostream>#include <cstdio>#include <vector>#include <cstring>using namespace std;vector<string> split(string& str,const char* c){    char *cstr,*p;    cstr = new char[str.size()+1];    strcpy(cstr,str.c_str());    vector<string> res;    p = strtok(cstr,c);    while(p!=NULL)    {        res.push_back(string(p));        p = strtok(NULL,c);    }    delete[] cstr;    return res;}int wordTOnum(string s){    if(s=="zero") return 0;    else if(s=="one") return 1;    else if(s=="two") return 2;    else if(s=="three") return 3;    else if(s=="four") return 4;    else if(s=="five") return 5;    else if(s=="six") return 6;    else if(s=="seven") return 7;    else if(s=="eight") return 8;    else if(s=="nine") return 9;}int main(){    int x,y;    bool afterAdd;    string str;    const char *delims=" ";    while(getline(cin,str))    {        x=y=0;        afterAdd=false;        vector<string> s;        s=split(str,delims);        if(s[0]=="zero"&&s[2]=="zero") break;        int len=s.size();        for(int i=0;i<len-1;i++)        {            if(s[i]=="+") afterAdd=true;            else            {                if(afterAdd) x=x*10+wordTOnum(s[i]);                else y=y*10+wordTOnum(s[i]);            }        }        cout<<x+y<<endl;    }}

 

作者: AlvinZH

出处: http://www.cnblogs.com/AlvinZH/

本文版权归作者AlvinZH和博客园所有,欢迎转载和商用,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

题目1010:A + B(字符串拆分)