求N进制的高精度加法
Input
第一行输入N(2≤N≤10)
第二行两个数X Y(长度均≤100)
Output
输出N进制下X和Y的和
Sample Input
5
2 4
Sample Output
11
输入应该是对应的N进制数,然后其它的地方类似于大数求和
以字符串形式输入,然后一位位导入到数组(逆向)
然后对应位数求和(注意进位)
最后判断最后一位是否是进位的情况
输出
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int m;
string s1,s2;
vector<int> a,b,s3;
while(cin>>s1>>s2>>m)
{
for(int i = s1.size() - 1;i >= 0;i--)
{
a.push_back(int(s1[i] - '0'));
}
for(int i = s2.size() - 1;i >= 0;i--)
{
b.push_back(int(s2[i] - '0'));
}
// for(int i = 0; i < a.size();i++)
// {
// cout<<a[i]<<" ";
// }
int lenmax = max(a.size(),b.size());
//cout<<lenmax<<endl;
int if_jinwei = 0;
for(int i = 0;i < lenmax;i++)
{
if(i >= a.size())
{
s3.push_back(b[i]);
if_jinwei = 0;
}
else if(i >= b.size())
{
s3.push_back(a[i]);
if_jinwei = 0;
}
else
{
s3.push_back((a[i] + b[i] + if_jinwei) % m );
//cout<<s3[i]<<endl;
if_jinwei = (a[i] + b[i] + if_jinwei) / m;
}
}
while(if_jinwei > m)
{
s3.push_back(if_jinwei % m);
if_jinwei /= m;
}
if(if_jinwei)
{
s3.push_back(if_jinwei);
}
// cout<<s3.size()<<endl;
for(int i = s3.size() - 1;i >= 0;i--)
{
cout<<s3[i];
}
cout<<endl;
}
}