#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include <cstdio>
using namespace std;
vector<int> add(vector<int> &A, vector<int> &B, int &h)
{
vector<int> C;
int t = 0;
for(int i = 0; i < A.size() || i < B.size(); i++)
{
if(i < A.size()) t += A[i];
if(i < B.size()) t += B[i];
C.push_back(t % h);//h进制
t /= h;
}
if(t) C.push_back(1);
return C;
}
int main()
{
string a, b;
int h;
cin >> h;
vector<int> A, B;
cin >> a >> b;
for(int i = a.size() - 1; i >= 0; i--)
(a[i] >= '0' && a[i] <= '9') ? A.push_back(a[i] - '0') : A.push_back(a[i] - 'A' + 10);//若该进制为大写字母,将其转化为相应的数字
for(int i = b.size() - 1; i >= 0; i--)
(b[i] >= '0' && b[i] <= '9') ? B.push_back(b[i] - '0') : B.push_back(b[i] - 'A' + 10);
vector<int> C = add(A, B, h);
for(int i = C.size() - 1; i >= 0; i--)
if (C[i] > 9)
cout << char(C[i] - 10 + 'A');//进行判断输出,将> 9的数转化为字母输出
else
cout << C[i];
return 0;
}
P1604 B进制星球——洛谷(高精度 C++)
最新推荐文章于 2023-04-02 02:21:33 发布