当前位置:网站首页>Addition and multiplication of large integers;
Addition and multiplication of large integers;
2022-06-12 02:57:00 【HENU_ Lzey】
Addition and multiplication of large integers ;
1. Add
#include<bits/stdc++.h>
using namespace std;
int a[10000],b[10000],c[10000];
char a1[10000],b1[10000];
int cnt,i,j;
int main(){
cin>>a1>>b1;
int lena=strlen(a1);
int lenb=strlen(b1);
int len=max(lena,lenb)+1;
for(i=1;i<=lena;i++)a[i]=a1[lena-i]-'0';
for(i=1;i<=lenb;i++)b[i]=b1[lenb-i]-'0';
for(i=1;i<=len;i++){
c[i]+=a[i]+b[i];
c[i+1]=c[i]/10;
c[i]=c[i]%10;
}
while(c[len]==0&&len>1)len--;
for(i=len;i>=1;i--)cout<<c[i];
return 0;
}
Multiplication of large integers ;
#include<bits/stdc++.h>
using namespace std;
char a1[10001],b1[10001];// This array is defined for ease of input ;
int a[10001],b[10001],c[10001];
int i,len,j;
int main ()
{
cin>>a1>>b1;//
int lena=strlen(a1);//
int lenb=strlen(b1);//
for(i=1;i<=lena;i++)a[i]=a1[lena-i]-'0';// Reverse input ;
for(i=1;i<=lenb;i++)b[i]=b1[lenb-i]-'0';// Reverse input ;
for(i=1;i<=lenb;i++)
for(j=1;j<=lena;j++)
c[i+j-1]+=a[j]*b[i];// When calculating the multiplication, do not consider the carry first , Then add it up .
for(i=1;i<lena+lenb;i++)//j It refers to the normal calculation carry ( Multiply bit by bit ), and i It is what we call dislocation addition .
if(c[i]>9)// Then consider carry ;
{
c[i+1]+=c[i]/10;
c[i]%=10;
}
len=lena+lenb;
while(c[len]==0&&len>1)len--;// Remove leading zeros ;
for(i=len;i>=1;i--)cout<<c[i];// Output in reverse order ;
return 0;
}
边栏推荐
- Force deduction solution summary 953 verification of alien language dictionary
- Min25 sieve
- Force deduction solution summary - Sword finger offer II 114 Alien dictionary
- Requirements and business model innovation - Requirements 7- user requirements acquisition based on use case / scenario model
- Penetration test - file upload
- The road of global evolution of vivo global mall -- multilingual solution
- How to make div 100% page (not screen) height- How to make a div 100% of page (not screen) height?
- [point cloud compression] variable image compression with a scale hyperprior
- 微信小程序项目实例——双人五子棋
- Android HTML5 page load cache optimization
猜你喜欢

Requirements and business model innovation - Requirements 7- user requirements acquisition based on use case / scenario model

Introduction to architecture - who moved my cake

Maya foreground rendering plug-in Mel scripting tool

ARD3M电动机保护器在煤炭行业中的应用

Wechat applet project example - I have a paintbrush (painting)

Inverted string - two solutions

Wechat applet project example - Fitness calculator

Sparse tensor based point cloud attribute compression

推荐6款办公软件,好用还免费,效率翻倍

Demand and business model innovation - demand 10- observation and document review
随机推荐
Hypergraph tilted data is merged into root node and transferred to 3dfiles
如何防止商场电气火灾的发生?
Interpreting Julia's 2021: step by step towards the mainstream programming language
Introduction to architecture - who moved my cake
errno: -4078, code: ‘ECONNREFUSED‘, syscall: ‘connect‘, address: ‘127.0.0.1‘, port: 3306; Postman error
WPS表格 学习笔记 - 高亮显示重复值
跨域有哪些解决方法?
Demand and business model innovation - demand 10- observation and document review
About 100 to realize the query table? Really? Let's experience the charm of amiya.
DbNull if statement - DbNull if statement
Selection (046) - what is the output of the following code?
Force deduction programming problem - solution summary
Interpreting 2021 of middleware: after being reshaped by cloud nativity, it is more difficult to select models
Force deduction solution summary 875- coco who likes bananas
$. map(data,function(item,index){return XXX})
El upload upload file
Transformation of geographical coordinates of wechat official account development
MySQL partition table create delete modify view
Proxy and reflection (II)
安科瑞抗晃电产品在河北某化工项目的应用