题目描述:
2个大整数相加,每一位是一个小写字母,二十六进制,a表示0,z表示25,求结果;
例:
输入:
z
bc
输出:
cb
C++代码:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string a, b, s;
while (cin >> a >> b)
{
s.clear();
int jin = 0;
int i = a.size() - 1, j = b.size() - 1;
for (; i >= 0 && j >= 0; --i, --j)
{
int nowa = a[i] - 'a';
int nowb = b[j] - 'a';
int now = (nowa + nowb + jin) % 26;
jin = (nowa + nowb) / 26;
s = (char)(now+'a') + s;
}
if (i == -1 && j!=-1)
{
while (j >= 0)
{
int nowb = b[j] - 'a';
int now = (nowb + jin) % 26;
jin = nowb / 26;
s = (char)(now + 'a') + s;
--j;
}
if (jin != 0)
{
s = (char)(jin + 'a') + s;
}
}
else if (j == -1 && i != -1)
{
while (i >= 0)
{
int nowb = b[i] - 'a';
int now = (nowb + jin) % 26;
jin = nowb / 26;
s = (char)(now + 'a') + s;
--i;
}
if (jin != 0)
{
s = (char)(jin + 'a') + s;
}
}
else if(i==-1 && j==-1)
{
if (jin != 0)
{
s = (char)(jin + 'a') + s;
}
}
cout << s << endl;
}
return 0;
}
注:代码未经过大量测试用例测试,仅供参考相互学习,欢迎指正优化;