当前位置:网站首页>【高精度】X进制整数加法
【高精度】X进制整数加法
2022-06-11 19:27:00 【MC快乐的苦Xiao怕】
题目:
题目描述
计算x进制整数a+b的值,( 0 <= a, b <= 10^100);
输入格式
第一行:一个整数x( 2 <=x<=10 || x=16),表示第二行的两个整数位x进制数。
第二行:两个用空格分割的x进制整数a和b.
输出格式
一行:a+b的值 (按原进制输出)
输入输出样列
输入样例1:
10
10 15
输出样例1:
25
输入样例2:
16
A B
输出样例2:
15
思路:
在高精度模板上改:
需要改的地方:
① 逆序存储时需要提前转成x进制
② 计算时的%10和/10改成%x和/x
③ 保存计算结果的时候要转成x进制
CODE:
#include <bits/stdc++.h>
using namespace std;
const int N = 10010 ;
int a[N] , b[N] , c[N] ;
string ans ;
string add(string as , string bs , int x)
{
ans.clear () ;
memset (a , 0 , sizeof (a)) ;
memset (b , 0 , sizeof (b)) ;
memset (c , 0 , sizeof (c)) ;
int alen = as.size () , blen = bs.size () , clen = max (alen , blen) + 1 ;
for (int i = 1; i <= alen; i++)
{
if (as[alen - i] >= '0' && as[alen - i] <= '9')
a[i] = as[alen - i] - '0' ;
else
a[i] = as[alen - i] - 'A' + 10 ;
}
for (int i = 1; i <= blen; i++)
{
if (bs[blen - i] >= '0' && bs[blen - i] <= '9')
b[i] = bs[blen - i] - '0' ;
else
b[i] = bs[blen - i] - 'A' + 10 ;
}
for (int i = 1; i < clen; i++)
{
c[i] += a[i] + b[i] ;
c[i + 1] = c[i] / x ;
c[i] %= x ;
}
while (c[clen] == 0 && clen > 1)
{
clen -- ;
}
for (int i = clen; i >= 1; i --)
{
if (c[i] >= 0 && c[i] <= 9)
ans += c[i] + '0' ;
else
ans += c[i] - 10 + 'A' ;
}
return ans ;
}
int main()
{
string a , b ;
int x ;
cin >> x >> a >> b ;
cout << add (a , b , x) ;
return 0;
}
AC了
边栏推荐
- Internet_ Business Analysis Overview
- Use Mysql to determine the day of the week
- leetcode:66. add one-tenth
- 关于富文本储存数据库格式转译问题
- YOLOv3 Pytorch代码及原理分析(一):跑通代码
- The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!
- Pyramid test principle: 8 tips for writing unit tests
- postman配置中文
- 使用图像处理技术和卷积神经网络(CNN)的作物病害检测
- 给你一个项目,你将如何开展性能测试工作?
猜你喜欢

Web3 Games: exploring and reshaping the game experience
![[Multisim Simulation] using operational amplifier to generate sawtooth wave](/img/98/27086746dc552ada25fd36a82cb52b.png)
[Multisim Simulation] using operational amplifier to generate sawtooth wave

CMU 15-445 database course lesson 5 text version - buffer pool

Chrome tips - browser web page setting coding, solving the problem of web page garbled code, obtaining the latest version of charset plug-in, UTF-8 coding setting
CMU 15-445 database course lesson 5 text version - buffer pool
![[Lao Wang's fallacy of brain science] Why do blind people](/img/7c/98f27bb55a1a3b74c0ed8fd7fd2cc5.jpg)
[Lao Wang's fallacy of brain science] Why do blind people "seem" to be more "sensitive" than normal people?
![[image denoising] impulse noise image denoising based on absolute difference median filter, weighted median filter and improved weighted median filter with matlab code attached](/img/dc/6348cb17ca91afe39381a9b3eb54e6.png)
[image denoising] impulse noise image denoising based on absolute difference median filter, weighted median filter and improved weighted median filter with matlab code attached

Today's sleep quality record is 60 points

Qubicle notes: Hello voxel

Programmers have changed dramatically in 10 years. Everything has changed, but it seems that nothing has changed
随机推荐
Image transformation torchvision Transforms notes
[assembly] analysis of Experiment 7 of the fourth edition of assembly language
[untitled]
On the translation of rich text storage database format
Performance of MOS transistor 25n120 of asemi in different application scenarios
Pyramid test principle: 8 tips for writing unit tests
collect. stream(). Use of the collect() method
[Lao Wang's fallacy of brain science] Why do blind people "seem" to be more "sensitive" than normal people?
Web3游戏:游戏体验的探寻与重塑
Flask CKEditor 富文本编译器实现文章的图片上传以及回显,解决路径出错的问题
Kubernetes binary installation (v1.20.15) (VIII) deploying network plug-ins
Understand how to get started with machine learning to quantify transactions?
Raki's notes on reading paper: memory replace with data compression for continuous learning
7-3 combinatorial problems (*)
Introduction to go language (VI) -- loop statement
highcharts设置柱状图宽度、渐变、圆角、柱子上方数据
Cf:b. array determinations
Crop disease detection using image processing technology and convolutional neural network (CNN)
【Laravel系列7.5】事件系统
Introduction to go language (V) -- branch statement