当前位置:网站首页>[high precision] decimal integer addition
[high precision] decimal integer addition
2022-06-11 19:38:00 【MC happy bitter Xiao afraid】
subject :
Title Description
Calculation x Hexadecimal integer a+b Value ,( 0 <= a, b <= 10^100);
Input format
first line : An integer x( 2 <=x<=10 || x=16), Represents the two integer digits of the second line x Hexadecimal number .
The second line : Two separated by spaces x Hexadecimal integer a and b.
Output format
a line :a+b Value ( Output in original decimal )
Input and output sample Columns
sample input 1:
10
10 15
sample output 1:
25
sample input 2:
16
A B
sample output 2:
15
Ideas :
Change on the high-precision template :
What needs to be changed :
① When storing in reverse order, it needs to be converted to x Base number
② Calculation %10 and /10 Change to %x and /x
③ When saving the calculation results, you should convert them to x Base number
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 了
边栏推荐
- 金字塔测试原理:写好单元测试的8个小技巧,一文总结
- [image segmentation] image segmentation based on Markov random field with matlab code
- Find the maximum 3 same digits in the string
- 程序员10年巨变,一切都变了又好像没变...
- AHB_Bus_Matrix_3x3 设计
- RTL仲裁器设计
- Use Mysql to determine the day of the week
- [Lao Wang's fallacy of brain science] Why do blind people "seem" to be more "sensitive" than normal people?
- LDPC 7 - simple example of decoding
- Unsupervised image classification code analysis notes of scan:learning to classify images without (1): simclr
猜你喜欢

2022 the latest software testing classic summarized by major manufacturers. After reading it, I'm not afraid I won't get an offer

Hyper parameter optimization of deep neural networks using Bayesian Optimization

AHB2Standard_handshake_bridge 设计

Qubicle notes: Hello voxel

金字塔测试原理:写好单元测试的8个小技巧,一文总结

Performance of MOS transistor 25n120 of asemi in different application scenarios

Go语言入门(五)——分支语句

Detailed explanation of iSCSI (IV) -- actual configuration of iSCSI server

RTL仲裁器设计

Operator new and placement new
随机推荐
556. next larger element iii- (31. next permutation) - two iterations
何恺明团队的“视频版本MAE”,高效视频预训练!Mask Ratio高达90%时效果也很好!...
Qubicle notes: self set shortcut keys (attached with Lao Wang's self set shortcut key file)
2022 the latest software testing classic summarized by major manufacturers. After reading it, I'm not afraid I won't get an offer
Specific methods for porting WinCC flexible 2008 project to botu WinCC
计算926的9260次方里的字符串里有多少个926
Judge whether it is a balanced binary tree
iMeta | 南科大夏雨组纳米孔测序揭示微生物可减轻高海拔冻土温室气体排放
Performance of MOS transistor 25n120 of asemi in different application scenarios
[untitled]
[signal denoising] signal denoising based on FFT and fir with matlab code
mysql 联合索引和BTree
postman配置中文
Go语言入门(五)——分支语句
On the selection and design of management subsystem of collection system
Introduction to ieda bottom menu
Hanging memory recursive dynamic programming (with example explanation POJ 1163)
【 aide 】 comment puis - je faire en sorte que les messages sélectionnés ci - dessous puissent être affichés après l'ouverture de l'article Wechat public number dans un navigateur externe?
Major work title and requirements of engineering earthquake resistance in autumn 21 [standard answer]
[image segmentation] image segmentation based on Markov random field with matlab code