当前位置:网站首页>High precision operation
High precision operation
2022-07-06 17:59:00 【Stellaris_ L】
List of articles
High precision addition
analysis
The main ways to realize high-precision addition are 2 spot :① How to save the number ② How to calculate logarithm
One 、 How to save a large integer ?
answer : Exists in the array , And from a bit ( Low position ) Start saving . For example, a number 123456789.
| Location | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| Number of deposits | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
(1) Why reverse order ?
answer : Every time you calculate , May encounter carry problems . If a carry occurs during the calculation of the largest bit , And we use positive order storage , You need to move the array to make a space at the beginning , This will be very cumbersome . Therefore, it is very convenient to add the last carry by directly using reverse order storage .
And this is also in line with the direction of our manual calculation .( Manual calculation will be explained later )
(2) Why use dynamic arrays (vector)
answer : In dynamic arrays , have access to STL The self-contained function is very convenient to obtain the array length , No external summation storage , And random access to .
Two 、 How to calculate an array ?
answer : Simulate artificial addition , Add from the low order of two numbers , When the sum is less than 10 Time is directly expressed as the operation result of this bit , The sum is greater than or equal to 10 Will be greater than 10 As the result of the operation of this bit and move forward one bit .
Templates
vector<int> add(string a,string b){
vector<int>A,B;// Store the string in reverse order into the dynamic array .
for(int i=a.size()-1;i>=0;i--)A.push_back(a[i]-'0');// Transformation types
for(int i=b.size()-1;i>=0;i--)B.push_back(b[i]-'0');
vector<int>C;
int t=0;// Carry sign , Initial carry 0
for(int i=0;i<A.size()||i<B.size();i++){
if(i<A.size())t+=A[i];// The current bit exists , Join in t
if(i<B.size())t+=B[i];
C.push_back(t%10);// Add non carry numbers to the array .
t/=10;// When t Greater than 10 Time quotient 1, That is, carry .
}
if(t)C.push_back(1);// Determine whether the last carry
return C;
}
Example
791. High precision addition - AcWing Question bank
AC Answer key :
#include <iostream>
#include <vector>
using namespace std;
vector<int> add(string a,string b){
vector<int>A,B;// Store the string in reverse order into the dynamic array .
for(int i=a.size()-1;i>=0;i--)A.push_back(a[i]-'0');// Transformation types
for(int i=b.size()-1;i>=0;i--)B.push_back(b[i]-'0');
vector<int>C;
int t=0;// Carry sign , Initial carry 0
for(int i=0;i<A.size()||i<B.size();i++){
if(i<A.size())t+=A[i];// The current bit exists , Join in t
if(i<B.size())t+=B[i];
C.push_back(t%10);// Add non carry numbers to the array .
t/=10;// When t Greater than 10 Time quotient 1, That is, carry .
}
if(t)C.push_back(1);// Determine whether the last carry
return C;
}
int main(){
string a,b;
cin>>a>>b;
auto sum=add(a,b);//auto You can automatically determine the type of return value , Can be lazy .
for(int i=sum.size()-1;i>=0;i--)cout<<sum[i];
return 0;
}
High precision subtraction
analysis
The storage methods of the four high-precision algorithms are the same .
| A 3 A_3 A3 | A 2 A_2 A2 | A 1 A_1 A1 | A 0 A_0 A0 | |
|---|---|---|---|---|
| — | B 2 B_2 B2 | B 1 B_1 B1 | B 0 B_0 B0 |
A i − B i − t A_i-B_i-t Ai−Bi−t There are two situations ,
① A i − B i − t ≥ 0 A_i-B_i-t\ge0 Ai−Bi−t≥0 There is direct A i − B i − t A_i-B_i-t Ai−Bi−t.
② A i − B i − t < 0 A_i-B_i-t<0 Ai−Bi−t<0 Yes A i − B i + 10 − t A_i-B_i+10-t Ai−Bi+10−t, At the same time, record a debit .
At this time, you can find a problem , Only when guaranteed A ≥ B A\ge B A≥B When can we calculate in a correct order . So we also need to consider being A < B A<B A<B The situation of , This situation is also easy to deal with, just change it to − ( B − A ) -(B-A) −(B−A) That's all right. . Of course, there is another point here , Namely A A A and B B B Must be positive numbers , If there is a negative number, it still needs a judgment to transform .
- summary : Judge A A A and B B B The size of the relationship , A ≥ B A\ge B A≥B When A − B A-B A−B; A < B A<B A<B When − ( B − A ) -(B-A) −(B−A).
Finally, we need to remove the leading 0.
Templates
bool cmp(vector<int> &A,vector<int> &B){
if(A.size()!=B.size())return A.size()>B.size();
for(int i=A.size()-1;i>=0;i--)
if(A[i]!=B[i])return A[i]>B[i];
return true;
}
vector<int> sub(vector<int> &A,vector<int> &B){
vector<int> C;
for(int i=0,t=0;i<A.size();i++){
t=A[i]-t;
if(i<B.size())t-=B[i];
C.push_back((t+10)%10);
if(t<0)t=1;
else t=0;
}
while(C.size()>1&&C.back()==0)C.pop_back();// Remove the lead 0
return C;
}
vector<int> C;
if(cmp(A,B))C=sub(A,B);
else C=sub(B,A),cout<<'-';
Example
792. High precision subtraction - AcWing Question bank
#include <iostream>
#include <vector>
using namespace std;
bool cmp(vector<int> &A,vector<int> &B){
if(A.size()!=B.size())return A.size()>B.size();
for(int i=A.size()-1;i>=0;i--)
if(A[i]!=B[i])
return A[i]>B[i];
return true;
}
vector<int> sub(vector<int> &A,vector<int> &B){
vector<int> C;
for(int i=0,t=0;i<A.size();i++){
t=A[i]-t;
if(i<B.size())t-=B[i];
C.push_back((t+10)%10);
if(t<0)t=1;
else t=0;
}
while(C.size()>1&&C.back()==0)C.pop_back();// Remove the lead 0
return C;
}
int main(){
string a,b;
vector<int> A,B;
cin>>a>>b;
for(int i=a.size()-1;i>=0;i--)A.push_back(a[i]-'0');
for(int i=b.size()-1;i>=0;i--)B.push_back(b[i]-'0');
vector<int> C;
if(cmp(A,B))C=sub(A,B);
else C=sub(B,A),cout<<'-';
for(int i=C.size()-1;i>=0;i--)cout<<C[i];
cout<<endl;
return 0;
}
High precision multiplication
analysis
The high-precision multiplication here is different from the previous high-speed addition and division , Not two high-precision phase operations . But a high precision and a low precision operation . The low accuracy here is less than m a x ( l o n g l o n g ) / 10 max(long\ long)/10 max(long long)/10 Of .
| A 2 = 1 A_2=1 A2=1 | A 1 = 2 A_1=2 A1=2 | A 0 = 3 A_0=3 A0=3 | ||
|---|---|---|---|---|
| × × × | B = 12 B=12 B=12 | |||
| carry | t 3 t_3 t3 | t 2 t_2 t2 | t 1 t_1 t1 | t 0 t_0 t0 |
| value | C 3 C_3 C3 | C 2 C_2 C2 | C 1 C_1 C1 | C 0 C_0 C0 |
First step :
C 0 = ( A 0 ∗ B + t 0 ) % 10 = 6 C_0=(A_0*B+t_0)\%10=6 C0=(A0∗B+t0)%10=6
t 1 = ( A 0 ∗ B + t 0 ) / 10 = 0 t_1=(A_0*B+t_0)/10=0 t1=(A0∗B+t0)/10=0
The second step :
C 1 = ( A 1 ∗ B + t 1 ) % 10 = 7 C_1=(A_1*B+t_1)\%10=7 C1=(A1∗B+t1)%10=7
t 2 = ( A 1 ∗ B + t 1 ) / 10 = 2 t_2=(A_1*B+t_1)/10=2 t2=(A1∗B+t1)/10=2
The third step :
C 2 = ( A 2 ∗ B + t 2 ) % 10 = 4 C_2=(A_2*B+t_2)\%10=4 C2=(A2∗B+t2)%10=4
t 3 = ( A 2 ∗ B + t 2 ) / 10 = 1 t_3=(A_2*B+t_2)/10=1 t3=(A2∗B+t2)/10=1
Step four :
C 3 = ( 0 ∗ B + t 3 ) % 10 = 1 C_3=(0*B+t_3)\%10=1 C3=(0∗B+t3)%10=1
- The final answer 123 ∗ 12 = 1476 123*12=1476 123∗12=1476
Templates
vector<int> mul(vector<int> &A,int b){
vector<int> C;
int t=0;
for (int i=0;i<A.size()||t;i++){
if(i<A.size())t+=A[i]*b;
C.push_back(t%10);
t/=10;
}
while(C.size()>1&&C.back()==0)C.pop_back();// Remove the lead 0
return C;
}
vector<int> A;
for(int i=a.size()-1;i>=0;i--)A.push_back(a[i]-'0');
Example
793. High precision multiplication - AcWing Question bank
#include <iostream>
#include <vector>
using namespace std;
vector<int> mul(vector<int> &A,int b){
vector<int> C;
int t=0;
for (int i=0;i<A.size()||t;i++){
if(i<A.size())t+=A[i]*b;
C.push_back(t%10);
t/=10;
}
while(C.size()>1&&C.back()==0)C.pop_back();// Remove the lead 0
return C;
}
int main(){
string a;
int b;
cin>>a>>b;
vector<int> A;
for(int i=a.size()-1;i>=0;i--)A.push_back(a[i]-'0');
auto C=mul(A,b);
for(int i=C.size()-1;i>=0;i--) printf("%d",C[i]);
return 0;
}
High precision division
analysis
Just like high-precision multiplication , High precision division is also a high precision and low precision operation . And the setting will not produce decimals , It exists in the form of remainder .
Templates
vector<int> div(vector<int> &A,int b,int &r){
//r Is reference passing
vector<int> C;// merchant
r=0;
for (int i=A.size()-1;i>=0;i--){
// Start at the top
r=r*10+A[i];
C.push_back(r/b);
r%=b;
}
reverse(C.begin(),C.end());
while(C.size()>1&&C.back()==0)C.pop_back();
return C;
}
Example
794. High precision division - AcWing Question bank
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> div(vector<int> &A,int b,int &r){
//r Is reference passing
vector<int> C;// merchant
r=0;
for (int i=A.size()-1;i>=0;i--){
// Start at the top
r=r*10+A[i];
C.push_back(r/b);
r%=b;
}
reverse(C.begin(),C.end());
while(C.size()>1&&C.back()==0)C.pop_back();
return C;
}
int main(){
string a;
vector<int> A;
int B;
cin>>a>>B;
for(int i=a.size()-1;i>=0;i--)A.push_back(a[i]-'0');
int r;
auto C=div(A,B,r);
for(int i=C.size()-1;i>=0;i--)cout<<C[i];
cout<<endl<<r<<endl;
return 0;
}
边栏推荐
- 基于STM32+华为云IOT设计的智能路灯
- OpenCV中如何使用滚动条动态调整参数
- 高精度运算
- Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
- 历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
- Compile and build, from the bottom to the top
- EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
- 传统家装有落差,VR全景家装让你体验新房落成效果
- 面试突击63:MySQL 中如何去重?
- FlutterWeb浏览器刷新后无法回退的解决方案
猜你喜欢

Getting started with pytest ----- allow generate report
![[translation] principle analysis of X Window Manager (I)](/img/40/6e15e1acebb47061d6e0e4c8ff82ea.jpg)
[translation] principle analysis of X Window Manager (I)

Olivetin can safely run shell commands on Web pages (Part 1)

FlutterWeb浏览器刷新后无法回退的解决方案

It doesn't make sense without a distributed gateway
![[ASM] introduction and use of bytecode operation classwriter class](/img/0b/87c9851e577df8dcf8198a272b81bd.png)
[ASM] introduction and use of bytecode operation classwriter class

带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities

Unity小技巧 - 绘制瞄准准心

There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house

Reppoints: advanced order of deformable convolution
随机推荐
How to use scroll bars to dynamically adjust parameters in opencv
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...
微信小程序获取手机号
Remote code execution penetration test - B module test
The art of Engineering (3): do not rely on each other between functions of code robustness
PyTorch 提取中间层特征?
Zen integration nails, bugs, needs, etc. are reminded by nails
面试突击62:group by 有哪些注意事项?
C # nanoframework lighting and key esp32
RepPoints:可形变卷积的进阶
EasyCVR授权到期页面无法登录,该如何解决?
Compile and build, from the bottom to the top
78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
sql语句优化,order by desc速度优化
Jerry's watch deletes the existing dial file [chapter]
VR全景婚礼,帮助新人记录浪漫且美好的场景
DNS hijacking
The difference between parallelism and concurrency
Pytest learning ----- pytest confitest of interface automation test Py file details