当前位置:网站首页>学习记录——高精度加法和乘法
学习记录——高精度加法和乘法
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;
}
边栏推荐
- Check the example of where the initialization is when C initializes the program
- Phpcms realizes PC website access to wechat native payment
- The request object parses the request body and request header parameters
- VS Code指定扩展安装位置
- Why does the starting service report an error when installing MySQL? (operating system Windows)
- “十二星座女神降临”全新活动推出
- Can I open a stock trading account online? Is it safe
- 字节跳动 Kitex 在森马电商场景的落地实践
- Internship log - day04
- 【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
猜你喜欢
随机推荐
Huffman encoded compressed file
Pytest learning - dayone
Flex flexible layout
ISP、IAP、ICP、JTAG、SWD的编程特点
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
2016 CCPC Hangzhou Onsite
Google colab loads Google drive (Google drive is used in Google colab)
Postman interface test V
Internship log - day07
Deep understanding of UDP, TCP
位操作==c语言2
Postman interface test VI
中国首款电音音频类“山野电音”数藏发售来了!
Pit using BigDecimal
The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
ORM--数据库增删改查操作逻辑
网上可以开炒股账户吗安全吗
Performance optimization record of the company's product "yunzhujia"
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
Some test points about coupon test