高精度加法
例 1
例如: 1111111111111+9, 列成豎式
,
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 9
---------------------------
先算個位,
1
+9
=10
,
滿10
, 向十位進(jìn)1
。
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 9
1
---------------------------
0
接下來, 處理進(jìn)位。
十位: 1+1=2 -> 2
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 9
1
---------------------------
2 0
百位: 無進(jìn)位, 直接照抄. 1 -> 1
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 9
1
---------------------------
1 2 0
千位: 1 -> 1
萬位: ...
...: ...
最高位: 1 -> 1
最終結(jié)果:
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 9
1
---------------------------
1 1 1 1 1 1 1 1 1 1 1 2 0
所以, 1111111111111+9=1111111111120
例 2
1111111111111+8888888888889,
這個算式變成了高精度
+高精度
了。
還是列成豎式,
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 8 8 8 8 8 8 8 8 8 8 8 8 9
---------------------------
先算個位,
1
+9
=10
,
滿10
, 向十位進(jìn)1
。
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 8 8 8 8 8 8 8 8 8 8 8 8 9
1
---------------------------
0
接下來, 算十位。
1
+8
+1=10
,
滿10
, 向百位進(jìn)1
。
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 8 8 8 8 8 8 8 8 8 8 8 8 9
1 1
---------------------------
0 0
千位, 萬位...以此類推。
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 8 8 8 8 8 8 8 8 8 8 8 8 9
1 1 1
---------------------------
0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 8 8 8 8 8 8 8 8 8 8 8 8 9
1 1 1 1
---------------------------
0 0 0 0
......
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 8 8 8 8 8 8 8 8 8 8 8 8 9
1 1 1 1 1 1 1 1 1 1 1 1
---------------------------
0 0 0 0 0 0 0 0 0 0 0 0
最高位:1
+8
+1=10
,
向前一位進(jìn)1.
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 8 8 8 8 8 8 8 8 8 8 8 8 9
1 1 1 1 1 1 1 1 1 1 1 1 1
---------------------------
0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 8 8 8 8 8 8 8 8 8 8 8 8 9
1 1 1 1 1 1 1 1 1 1 1 1 1
---------------------------
1 0 0 0 0 0 0 0 0 0 0 0 0 0
所以,
1111111111111
+8888888888889
=10000000000000
=1013
1 1 1 1 1 1 1 1 1 1 1 1 1
+ 8 8 8 8 8 8 8 8 8 8 8 8 9
1 1 1 1 1 1 1 1 1 1 1 1 1
---------------------------
1 0 0 0 0 0 0 0 0 0 0 0 0 0
代碼
- 首先導(dǎo)入頭文件。
// 1. Import libraries
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <vector>
// Import namespace 'std'
using namespace std;
- 初始化變量。
// 2. Initialize variables
const int MAX = 1000;
char s[MAX+1];
int a[MAX+1], b[MAX+1],c[MAX+2];
- 定義主函數(shù)
main()
// 3. Define Main Function
int main(int argc, char **argv) {
for (int i=0;i<1001;i++) {
s[i]='0';
a[i]=0, b[i]=0, c[i]=0;
} // If you use global variables, this initialization does not need used.
這里如果使用全局變量,這個初始化可以不用。
4. 輸入(包含了處理)變量文章來源:http://www.zghlxwxcb.cn/news/detail-460267.html
// 4. Input and process variables
scanf("%s",s+1);
int lena = strlen(s+1);
for (int i=1;i<=lena+1;i++)
a[i] = s[lena-i+1] - '0';
scanf("%s",s+1);
int lenb = strlen(s+1);
for (int i=1;i<=lenb+1;i++)
b[i] = s[lenb-i+1] - '0';
int lenc = (lena>lenb)?lena:lenb;
- 真正的高精度計(jì)算到這里才開始。計(jì)算代碼
// 5. Calculate
for (int i=1;i<=101;i++)
c[i] = 0;
for (int i=1;i<=lenc;i++) {
// c[i] = a[i] + b[i]; <-- Wrong!
c[i] = a[i] + b[i] + c[i]; // <-- Correct
// c[i] += a[i] + b[i]; <-- Plan B, Correct too
// 處理進(jìn)位
c[i+1] = c[i] / 10;
c[i] = c[i] % 10;
}
// 處理 overflow
if (c[lenc+1]>0)
lenc++;
- 輸出
// 6. Output
for (int i=lenc;i>=1;i--)
printf("%d", c[i]);
return 0;
}
附: 完整代碼
// 1. Import libraries
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <vector>
// Import namespace 'std'
using namespace std;
// 2. Initialize variables
const int MAX = 1000;
char s[MAX+1];
int a[MAX+1], b[MAX+1],c[MAX+2];
// 3. Define Main Function
int main(int argc, char **argv) {
for (int i=0;i<1001;i++) {
s[i]='0';
a[i]=0, b[i]=0, c[i]=0;
} // If you use global variables, this initialization does not need used.
// 4. Input and process variables
scanf("%s",s+1);
int lena = strlen(s+1);
for (int i=1;i<=lena+1;i++)
a[i] = s[lena-i+1] - '0';
scanf("%s",s+1);
int lenb = strlen(s+1);
for (int i=1;i<=lenb+1;i++)
b[i] = s[lenb-i+1] - '0';
int lenc = (lena>lenb)?lena:lenb;
// 5. Calculate
for (int i=1;i<=101;i++)
c[i] = 0;
for (int i=1;i<=lenc;i++) {
// c[i] = a[i] + b[i]; <-- Wrong!
c[i] = a[i] + b[i] + c[i]; // <-- Correct
// c[i] += a[i] + b[i]; <-- Plan B, Correct too
// 處理進(jìn)位
c[i+1] = c[i] / 10;
c[i] = c[i] % 10;
}
// 處理 overflow
if (c[lenc+1]>0)
lenc++;
// 6. Output
for (int i=lenc;i>=1;i--)
printf("%d", c[i]);
return 0;
}
Control
, 縮寫為Ctrl, 擴(kuò)寫為唱,跳,rap,籃球。
你還敢Ctrl+C嗎?文章來源地址http://www.zghlxwxcb.cn/news/detail-460267.html
到了這里,關(guān)于高精度加法(含代碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!