当前位置:网站首页>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;
}
边栏推荐
- 【acwing】789. 数的范围(二分基础)
- 官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
- Internship log - day04
- Interface test
- MCU is the most popular science (ten thousand words summary, worth collecting)
- The Himalaya web version will pop up after each pause. It is recommended to download the client solution
- Appx代碼簽名指南
- 电表远程抄表拉合闸操作命令指令
- MCU与MPU的区别
- Inno setup packaging and signing Guide
猜你喜欢
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
fiddler-AutoResponder
Postman interface test III
Google colab loads Google drive (Google drive is used in Google colab)
ORM--分组查询,聚合查询,查询集QuerySet对象特性
【acwing】789. 数的范围(二分基础)
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
电表远程抄表拉合闸操作命令指令
随机推荐
The request object parses the request body and request header parameters
Differences between MCU and MPU
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
LeetCode 练习——113. 路径总和 II
.NET配置系统
The method of word automatically generating directory
STM32基础知识—内存映射
中国首款电音音频类“山野电音”数藏发售来了!
Pit encountered by vs2015 under win7 (successful)
Introduction to energy Router: Architecture and functions for energy Internet
MCU与MPU的区别
Postman interface test VI
高数_第1章空间解析几何与向量代数_向量的数量积
Bean 作⽤域和⽣命周期
Garbage disposal method based on the separation of smart city and storage and living digital home mode
Advanced function learning in ES6
ORM--数据库增删改查操作逻辑
Postman interface test I
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT