[STL][003]又见A + B
Time Limit: 1000 ms Memory Limit: 65536 KBTotal Submit: 55 Accepted: 48
Description
读入两个小于100的正整数A和B,计算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 =
three four + five six =
zero seven + eight nine =
zero + zero =
Sample Output
3
90
96
90
96
Hint
<map>
Source
浙大计算机研究生复试上机考试-2005年
虽然也算是水题了,但是计算的时候……靠!!!出了点错误,让我WA了三次,改了几遍,就是找不到错误的地方,最后实在是太无语了,无意中发现了一例有误的才明白计算有个地方出错了,无语死了……
#include <iostream>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
char a[1000],b[1000][10];
int main()
{
map<string,int>p;
p["one"]=1;
p["two"]=2;
p["three"]=3;
p["four"]=4;
p["five"]=5;
p["six"]=6;
p["seven"]=7;
p["eight"]=8;
p["nine"]=9;
p["zero"]=0;
while(gets(a))
{
string s,s1,s2;
int p1,p2,i,k=0,sum;
char a1[1000],a2[1000];
s=a;
p1=s.find("+");
p2=s.find("=");
s1=s.substr(0,p1-1);
s2=s.substr(p1+2,p2-3-p1);
if(s1=="zero"&&s2=="zero") break;
for(i=0; i<s1.length(); i++)
a1[i]=s1[i];
a1[i]='\0';
for(i=0; i<s2.length(); i++)
a2[i]=s2[i];
a2[i]='\0';
char *q1=strtok(a1," ");
while(q1!=NULL)
{
strcpy(b[k++],q1);
q1=strtok(NULL," ");
}
s1=b[k-1];
sum=p[s1];
if(k>1)
{
s1=b[k-2];
sum+=p[s1]*10;
}
char *q2=strtok(a2," ");
k=0;
while(q2!=NULL)
{
strcpy(b[k++],q2);
q2=strtok(NULL," ");
}
s2=b[k-1];
sum+=p[s2];
if(k>1)
{
s2=b[k-2];
sum+=p[s2]*10;
}
cout<<sum<<endl;
}
return 0;
}