20火星加法

来源:互联网 发布:电视直播大全软件 编辑:程序博客网 时间:2024/05/02 08:03
在 22 世纪,科学家发现,火星上有智慧的居民。火星人都非常喜欢数学。每年,他们都要在火星上举行一次算术竞赛(ACM)。竞赛的内容是计算两个100 位数字的和,该大奖的得主是使用时间最少的人。今年他们还请地球上的人参加竞赛。作为地球上唯一的代表,您被发送到火星上,以展示人类的力量。幸好你带上了你的笔记本电脑,它可以帮助你做得很快。现在剩下的问题是,你只需编写一个简短的程序来计算两个给定的数字的总和。不过,在开始编写程序前,要记住,火星人使用的是二十进制的数字系统,因为他们通常有20 根手指。
2.输入描述
给你若干对火星数字,每个数字占一行。火星数字由 0 到9,以及从a 到j 的小写字母组成(小写字母a 到j 分别代表10,
11,…,19)。给定的数字的长度不会超过 100 位。
3.输出描述
对于每对数字,在一行上输出它们的和。
4.输入样例
1234567890
abcdefghij
99999jjjjj
9999900001
5.输出样例
bdfi02467j
iiiij00000

#include "stdafx.h"#include<fstream>#include<iostream>#include<string>using namespace std;int  _tmain(int argc, _TCHAR* argv[]){ifstream in("D:\\visual studio 2013 code\\test.txt");string str1, str2,s,str="";int a, b, sum;int flag = 0;char ch=NULL;while (in >> str1 >> str2){//将str1存储较长的数字字符串if (str1.size() < str2.size()){s = str1;str2 = str1;str1 = s;}//将两个字符串反序reverse(str1.begin(), str1.end());reverse(str2.begin(), str2.end());for (int i = 0; i < str1.size(); i++){if (str1[i] == '0') a =0 ;else if (str1[i] == '1') a = 1;else if (str1[i] == '2') a =2 ;else if (str1[i] == '3') a =3 ;else if (str1[i] == '4') a = 4;else if (str1[i] == '5') a = 5;else if (str1[i] == '6') a = 6;else if (str1[i] == '7') a = 7;else if (str1[i] == '8') a = 8;else if (str1[i] == '9') a = 9;else if (str1[i] == 'a') a = 10;else if (str1[i] == 'b') a = 11;else if (str1[i] == 'c') a = 12;else if (str1[i] == 'd') a = 13;else if (str1[i] == 'e') a = 14;else if (str1[i] == 'f') a = 15;else if (str1[i] == 'g') a = 16;else if (str1[i] == 'h') a = 17;else if (str1[i] == 'i') a = 18;else if (str1[i] == 'j') a = 19;//cout << str2[i] ;if (i >= str2.size()) b = 0;else if (str2[i] == '0')b = 0;else if (str2[i] == '1')b = 1;else if (str2[i] == '2')b = 2;else if (str2[i] == '3')b = 3;else if (str2[i] == '4')b = 4;else if (str2[i] == '5')b = 5;else if (str2[i] == '6')b = 6;else if (str2[i] == '7')b = 7;else if (str2[i] == '8')b = 8;else if (str2[i] == '9')b = 9;else if (str2[i] == 'a')b = 10;else if (str2[i] == 'b')b = 11;else if (str2[i] == 'c')b = 12;else if (str2[i] == 'd')b = 13;else if (str2[i] == 'e')b = 14;else if (str2[i] == 'f')b = 15;else if (str2[i] == 'g')b = 16;else if (str2[i] == 'h')b = 17;else if (str2[i] == 'i')b = 18;else if (str2[i] == 'j')b = 19;sum = a + b + flag;//cout << sum << " ";if (sum > 19){flag = 1;sum -= 20;}else{flag = 0;}//cout << sum << " ";if (sum == 0) ch = '0';else if (sum == 1)ch = '1';else if (sum == 2)ch = '2';else if (sum == 3)ch = '3';else if (sum == 4)ch = '4';else if (sum == 5)ch = '5';else if (sum == 6)ch = '6';else if (sum == 7)ch = '7';else if (sum == 8)ch = '8';else if (sum == 9)ch = '9';else if (sum == 10)ch = 'a';else if (sum == 11)ch = 'b';else if (sum == 12)ch = 'c';else if (sum == 13)ch = 'd';else if (sum == 14)ch = 'e';else if (sum == 15)ch = 'f';else if (sum == 16)ch = 'g';else if (sum == 17)ch = 'h';else if (sum == 18)ch = 'i';else if (sum == 19)ch = 'j';//cout << ch << " ";str = str+ch;//cout << str<<" ";}reverse(str.begin(), str.end());cout << str << endl;str = "";}return 0;}


0 0
原创粉丝点击