当前位置:网站首页>学习记录——高精度加法和乘法
学习记录——高精度加法和乘法
2022-07-07 07:33:00 【昨夜太平长安_】
#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());//结果的位数
//反向保存
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';
//相加
for (int i = 1; i <= len; ++i)
{
C[i] += A[i] + B[i];
//处理进位
C[i + 1] = C[i] / 10;
C[i] %= 10;
}
if (C[len+1])//进位了则长度加1
len++;
for (int i = len ; i >= 1; --i)//反向输出
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();
//反向保存
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';
//计算贡献--a[i]*b[j]的结果在第i+j-1位上
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;//乘积的位数不超过两数位数之和
//处理进位
for (int i = 1; i <= len; ++i)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
//去掉前导0
while (!c[len])
len--;
//输出
for (int i = max(1,len); i >= 1; --i)
cout << c[i];
return 0;
}
边栏推荐
- Garbage disposal method based on the separation of smart city and storage and living digital home mode
- Win10 installation vs2015
- Horizontal split of database
- 官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
- Gym - 102219J Kitchen Plates(暴力或拓扑序列)
- The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
- request对象对请求体,请求头参数的解析
- Software modeling and analysis
- Internship log - day04
- CDZSC_ 2022 winter vacation personal training match level 21 (2)
猜你喜欢
喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
Postman interface test II
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
Flex flexible layout
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
高数_第1章空间解析几何与向量代数_向量的数量积
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)
Bean operation domain and life cycle
[untitled]
STM32中AHB总线_APB2总线_APB1总线这些是什么
随机推荐
The physical meaning of imaginary number J
Guys, how can mysql-cdc convert the upsert message to append only
Apprentissage avancé des fonctions en es6
Parameter sniffing (1/2)
ORM -- database addition, deletion, modification and query operation logic
CDZSC_ 2022 winter vacation personal training match level 21 (2)
The landing practice of ByteDance kitex in SEMA e-commerce scene
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
Or in SQL, what scenarios will lead to full table scanning
Writing file types generated by C language
Deep understanding of UDP, TCP
Bean 作⽤域和⽣命周期
Introduction to energy Router: Architecture and functions for energy Internet
网上可以开炒股账户吗安全吗
Advanced function learning in ES6
ISP、IAP、ICP、JTAG、SWD的编程特点
【ORM框架】
The new activity of "the arrival of twelve constellations and goddesses" was launched
Internship log - day07
Horizontal split of database