当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Es classes and objects, prototypes
【acwing】786. 第k个数
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
【二开】【JeecgBoot】修改分页参数
搭建物联网硬件通信技术几种方案
Embedded background - chip
Some thoughts on the testing work in the process of R & D
Win10安装VS2015
Web3.0 series distributed storage IPFs
随机推荐
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
ORM -- database addition, deletion, modification and query operation logic
Postman interface test III
Introduction to automated testing framework
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
STM32 Basics - memory mapping
Mongodb creates an implicit database as an exercise
request对象对请求体,请求头参数的解析
Postman interface test VI
web3.0系列之分布式存储IPFS
Postman interface test VII
Bean 作⽤域和⽣命周期
位操作==c语言2
MongoDB创建一个隐式数据库用作练习
基于gis三维可视化技术的智慧城市建设
Internship log - day04
How to cancel automatic saving of changes in sqlyog database
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
Deadlock caused by non clustered index in SQL Server
Embedded background - chip