当前位置:网站首页>Learning records - high precision addition and multiplication
Learning records - high precision addition and multiplication
2022-07-07 10:16:00 【Last night was peaceful and Chang'an_】
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 1000;
int A[maxn], B[maxn], C[maxn];
int main()
{
string a, b;
cin >> a >> b;
int len = max(a.size(), b.size());// Number of digits of the result
// Reverse save
for (int i = a.length() - 1; i >= 0; --i)
A[a.length()-i] = a[i] - '0';
for (int i = b.length() - 1; i >= 0; --i)
B[b.length()-i] = b[i] - '0';
// Add up
for (int i = 1; i <= len; ++i)
{
C[i] += A[i] + B[i];
// Processing carry
C[i + 1] = C[i] / 10;
C[i] %= 10;
}
if (C[len+1])// If it is carried, the length is added 1
len++;
for (int i = len ; i >= 1; --i)// Reverse output
cout << C[i];
return 0;
}
#include <iostream>
using namespace std;
const int maxn =10000;
int a[maxn], b[maxn], c[maxn];
int main()
{
string x, y;
cin >> x >> y;
int lx = x.length();
int ly = y.length();
// Reverse save
for (int i = lx - 1; i >= 0; --i)
a[lx - i] = x[i] - '0';
for (int i = ly - 1; i >= 0; --i)
b[ly - i] = y[i] - '0';
// Computation contribution --a[i]*b[j] The result of is in page i+j-1 On a
for (int i = 1; i <= lx; ++i)
for (int j = 1; j <= ly; ++j)
c[i + j - 1] += a[i] * b[j];
int len = lx + ly;// The number of digits of the product shall not exceed the sum of two digits
// Processing carry
for (int i = 1; i <= len; ++i)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
// Remove the lead 0
while (!c[len])
len--;
// Output
for (int i = max(1,len); i >= 1; --i)
cout << c[i];
return 0;
}
边栏推荐
猜你喜欢
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
Programming features of ISP, IAP, ICP, JTAG and SWD
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
The physical meaning of imaginary number J
Postman interface test VI
Win10安装VS2015
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
Agile course training
喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
随机推荐
Postman interface test IV
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
Some thoughts on the testing work in the process of R & D
phpcms实现PC网站接入微信Native支付
MCU与MPU的区别
Introduction to energy Router: Architecture and functions for energy Internet
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
Es classes and objects, prototypes
虚数j的物理意义
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
Fiddler simulates the interface test
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
A wave of open source notebooks is coming
uboot机构简介
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
Word自动生成目录的方法
位操作==c语言2
对存储过程进行加密和解密(SQL 2008/SQL 2012)
Writing file types generated by C language