您的位置:首页 > 理论基础

2005年浙江大学计算机及软件工程研究生机试真题 A + B 九度 1010

2013-03-30 17:42 519 查看
题目1010:A + B

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3268

解决:1704

题目描述:
读入两个小于100的正整数A和B,计算A+B.
需要注意的是:A和B的每一位数字由对应的英文单词给出.
输入:
测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.
输出:
对每个测试用例输出1行,即A+B的值.
样例输入:
one + two =
three four + five six =
zero seven + eight nine =
zero + zero =

样例输出:
3
90
96

来源:2005年浙江大学计算机及软件工程研究生机试真题

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;

int ToNumber(string A)
{
if(A=="one")
return 1;
else if(A=="two")
return 2;
else if(A=="three")
return 3;
else if(A=="four")
return 4;
else if(A=="five")
return 5;
else if(A=="six")
return 6;
else if(A=="seven")
return 7;
else if(A=="eight")
return 8;
else if(A=="nine")
return 9;
else if(A=="zero")
return 0;
else
return -1;
}
int main()
{
string num[10];
int i = 0;
//输入重定向,输入数据将从in.txt文件中读取
//freopen("in.txt","r",stdin);
while(cin>>num[i])
{
int num1 = 0,num2 = 0;
//遇到等号停止输入
if(num[i] != "="){
i++;
continue;
}
int j = 0;
while(num[j] != "+"){
num1 = num1 * 10 + ToNumber(num[j]);
j++;
}
while(++j<i){
num2 = num2 * 10 + ToNumber(num[j]);
}
i = 0;
if(num1 == 0 && num2 == 0){
break;
}
printf("%d\n",num1 + num2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐