当前位置:网站首页>学习记录——高精度加法和乘法
学习记录——高精度加法和乘法
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;
}
边栏推荐
- Or in SQL, what scenarios will lead to full table scanning
- 2020ccpc Weihai J - Steins; Game (SG function, linear basis)
- China's first electronic audio category "Yamano electronic audio" digital collection is on sale!
- 为什么安装mysql时starting service报错?(操作系统-windows)
- The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- The physical meaning of imaginary number J
- ES6中的原型对象
- MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
- Luogu p2482 [sdoi2010] zhuguosha
猜你喜欢
Garbage disposal method based on the separation of smart city and storage and living digital home mode
Delete a record in the table in pl/sql by mistake, and the recovery method
CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)
STM32中AHB总线_APB2总线_APB1总线这些是什么
Introduction to energy Router: Architecture and functions for energy Internet
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
Bean operation domain and life cycle
Postman interface test I
随机推荐
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
嵌入式背景知识-芯片
ORM--数据库增删改查操作逻辑
2016 CCPC Hangzhou Onsite
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
Win10安装VS2015
Agile course training
中国首款电音音频类“山野电音”数藏发售来了!
STM32产品介绍
【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
一大波开源小抄来袭
Flinkcdc failed to collect Oracle in the snapshot stage. How do you adjust this?
Why does the starting service report an error when installing MySQL? (operating system Windows)
CDZSC_ 2022 winter vacation personal training match level 21 (1)
ES6中的函数进阶学习
MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
The physical meaning of imaginary number J
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
第十四次试验
Pytest learning - dayone