目录
高精度加法 高精度减法 高精度乘法 高精度除法 附录
用程序来模拟竖式加法即可,注意在输出的时候除去多余的前导零。
#include
#include
#include
using namespace std;
const int MAX_SIZE = 3333;
inline void High_precision_Addition(int *a, int *b, int *c) {
c[0] = 1; int x = 0;
while (c[0] <= a[0] || c[0] <= b[0]) {
c[c[0]] = a[c[0]] + b[c[0]] + x;
x = c[c[0]] / 10;
c[c[0]] %= 10;
++c[0];
};
c[c[0]] = x;
while (c[c[0]] == 0 && c[0] > 1) --c[0];
};
int main(int argc, char *argv[]) {
char str1[MAX_SIZE], str2[MAX_SIZE];
cin >> str1 >> str2;
int a[MAX_SIZE], b[MAX_SIZE], c[MAX_SIZE];
memset(a, 0, sizeof a); memset(b, 0, sizeof b); memset(c, 0, sizeof c);
a[0] = strlen(str1); b[0] = strlen(str2);
for (int i = 0; i < a[0]; ++i) a[a[0] - i] = str1[i] - '0';
for (int i = 0; i < b[0]; ++i) b[b[0] - i] = str2[i] - '0';
High_precision_Addition(a, b, c);
for (int i = c[0]; i >= 1; --i) printf("%d", c[i]); cout.put('\n'); //和
return 0;
};
用程序来模拟竖式减法即可,与高精度加法类似,不过得考虑被减数与减数的大小关系还得考虑借位,所以代码相对于高精度加法更为复杂。
#include
#include
#include
using namespace std;
const int MAX_SIZE = 3333;
inline bool Is_swap(char *__x_, char *__y_) {
char Mid_Element[MAX_SIZE];
if ((strlen(__x_) < strlen(__y_)) || (strlen(__x_) == strlen(__y_) && strcmp(__x_, __y_) < 0)) {
strcpy(Mid_Element, __x_);
strcpy(__x_, __y_);
strcpy(__y_, Mid_Element);
return true;
};
return false;
};
inline void High_precision_Subtraction(int *a, int *b, int *c) {
c[0] = 1;
while (c[0] <= a[0] || c[0] <= b[0]) {
if (a[c[0]] < b[c[0]]) {
a[c[0]] += 10;
--a[c[0] + 1];
};
c[c[0]] = a[c[0]] - b[c[0]];
++c[0];
};
while (c[c[0]] == 0 && c[0] > 1) --c[0];
};
int main(int argc, char *argv[]) {
char str1[MAX_SIZE], str2[MAX_SIZE];
cin >> str1 >> str2; bool flag = Is_swap(str1, str2);
int a[MAX_SIZE], b[MAX_SIZE], c[MAX_SIZE];
memset(a, 0, sizeof a); memset(b, 0, sizeof b); memset(c, 0, sizeof c);
a[0] = strlen(str1); b[0] = strlen(str2);
for (int i = 0; i < a[0]; ++i) a[a[0] - i] = str1[i] - '0';
for (int i = 0; i < b[0]; ++i) b[b[0] - i] = str2[i] - '0';
High_precision_Subtraction(a, b, c);
if (flag == true) cout.put('-');
for (int i = c[0]; i >= 1; --i) printf("%d", c[i]); cout.put('\n'); //差
return 0;
};
用程序模拟竖式乘法即可,在做乘法运算时,同样也有进位,同时对每一位进行乘法运算时,必须进行错位相加。
#include
#include
#include
using namespace std;
const int MAX_SIZE = 3333;
inline void High_precision_Multiplication(int *a, int *b, int *c) {
int x;
for (int i = 1; i <= a[0]; ++i) {
x = 0;
for (int j = 1; j <= b[0]; ++j) {
c[i + j - 1] = a[i] * b[j] + x + c[i + j - 1];
x = c[i + j - 1] / 10;
c[i + j - 1] %= 10;
};
c[i + b[0]] = x;
};
c[0] = a[0] + b[0];
while (c[c[0]] == 0 && c[0] > 1) --c[0];
};
int main(int argc, char *argv[]) {
char str1[MAX_SIZE], str2[MAX_SIZE];
cin >> str1 >> str2;
int a[MAX_SIZE], b[MAX_SIZE], c[MAX_SIZE];
memset(a, 0, sizeof a); memset(b, 0, sizeof b); memset(c, 0, sizeof c);
a[0] = strlen(str1); b[0] = strlen(str2);
for (int i = 0; i < a[0]; ++i) a[a[0] - i] = str1[i] - '0';
for (int i = 0; i < b[0]; ++i) b[b[0] - i] = str2[i] - '0';
High_precision_Multiplication(a, b, c);
for (int i = c[0]; i >= 1; --i) cout << c[i]; cout.put('\n'); //积
return 0;
};
高精度除法一般有高精度除以低精度与高精度除以高精度,前者代码较为简单,后者则作用范围更加广阔。高精度除以低精度与前三种高精度算法类似,都是按位运算;而高精度除以高精度则是以减法来模拟除法。
#include
#include
#include
using namespace std;
const int MAX_SIZE = 3333;
inline int High_precision_Division(int *a, int b, int *c) {
int x = 0;
for (int i = 1; i <= a[0]; ++i) {
c[i] = (x * 10 + a[i]) / b;
x = (x * 10 + a[i]) % b;
};
c[0] = 1;
while (c[c[0]] == 0 && c[0] < a[0]) ++c[0];
return x;
};
int main(int argc, char *argv[]) {
char str[MAX_SIZE]; int b;
cin >> str >> b;
int a[MAX_SIZE], c[MAX_SIZE];
memset(a, 0, sizeof a); memset(c, 0, sizeof c);
a[0] = strlen(str);
for (int i = 0; i < a[0]; ++i) a[i + 1] = str[i] - '0';
int x = High_precision_Division(a, b, c);
for (int i = c[0]; i <= a[0]; ++i) cout << c[i]; cout.put('\n'); //商
cout << x << endl; //余数
return 0;
};
#include
#include
#include
#include
using namespace std;
const int MAX_SIZE = 3333;
inline void Input(int *radix) {
string str; cin >> str; radix[0] = str.length();
for (int i = 0; i < radix[0]; ++i) radix[radix[0] - i] = str[i] - '0';
};
inline void Print(const int *radix) {
if (radix[0] == 0) {cout << 0 << endl; return;};
for (int i = radix[0]; i >= 1; --i) printf("%d", radix[i]);
cout.put('\n'); return;
};
inline int Compare(const int *__x_, const int *__y_) {
if (__x_[0] > __y_[0]) return 1;
if (__x_[0] < __y_[0]) return -1;
for (int i = __x_[0]; i >= 1; --i) {
if (__x_[i] == __y_[i]) continue;
else if (__x_[i] > __y_[i]) return 1;
else if (__x_[i] < __y_[i]) return -1;
};
return 0;
};
inline void Subtraction(int *__x_, int *__y_) {
int sign = Compare(__x_, __y_);
if (sign == 0) {__x_[0] = 0; return;};
if (sign == 1) {
for (int i = 1; i <= __x_[0]; ++i) {
if (__x_[i] < __y_[i]) {
__x_[i] += 10; --__x_[i + 1];
};
__x_[i] -= __y_[i];
};
while (__x_[__x_[0]] == 0 && __x_[0] > 0) --__x_[0]; return;
};
};
inline void Numcpy(const int *radix, int *pending, int pos) {
for (int i = 1; i <= radix[0]; ++i) pending[i + pos - 1] = radix[i];
pending[0] = radix[0] + pos - 1;
};
inline void High_precision_Division(int *a, int *b, int *c) {
int sign = Compare(a, b);
if (sign == -1) {c[0] = 1; c[1] = 0; return;};
if (sign == 1 || sign == 0) {
int Temp[MAX_SIZE]; c[0] = a[0] - b[0] + 1;
for (int i = c[0]; i >= 1; --i) {
memset(Temp, 0, sizeof Temp); Numcpy(b, Temp, i);
while (Compare(a, Temp) != -1) {++c[i]; Subtraction(a, Temp);};
};
while (c[c[0]] == 0 && c[0] > 0) --c[0]; return;
};
};
int main(int argc, char *argv[]) {
int a[MAX_SIZE], b[MAX_SIZE], c[MAX_SIZE];
memset(a, 0, sizeof a); memset(b, 0, sizeof b); memset(c, 0, sizeof c);
Input(a); Input(b); High_precision_Division(a, b, c);
Print(c); Print(a); //商与余数
return 0;
};
//高精度模板,ctrl+c & ctrl+v拿走即可
//written by Takaway all rights reserved
#include
#include
#include
#include
#include
#include
using namespace std;
typedef short int INT16;
typedef unsigned short int UINT16;
typedef int INT32;
typedef long long int INT64;
typedef unsigned int UINT32;
typedef unsigned long long int UINT64;
struct BigInt {
const static INT32 MAX_SIZE = 1024;
static INT32 compare(INT32 *x, INT32 *y) {
if (x[0] > y[0]) return 1;
if (x[0] < y[0]) return -1;
for (INT32 i = x[0]; i >= 1; --i) {
if (x[i] == y[i]) continue;
else if (x[i] > y[i]) return 1;
else if (x[i] < y[i]) return -1;
};
return 0;
};
static void numcpy(INT32 *r, INT32 *p, INT32 &pos) {
p[0] = r[0] + pos - 1;
for (INT32 i = 1; i <= r[0]; ++i) p[i + pos - 1] = r[i];
};
static void subtraction(INT32 *x, INT32 *y) {
INT32 sign = compare(x, y);
if (sign == 0) {x[0] = 0; return;};
if (sign == 1) {
for (INT32 i = 1; i <= x[0]; ++i) {
if (x[i] < y[i]) {
x[i] += 10; --x[i + 1];
};
x[i] -= y[i];
};
while (x[x[0]] == 0 && x[0] > 0) --x[0];
};
};
INT32 data[MAX_SIZE]; char sign;
BigInt () {
memset(data, 0, sizeof data); data[0] = 1; sign = '+';
};
friend istream &operator >> (istream &is, BigInt &r) {
string str;
is >> str; r.data[0] = str.length();
for (INT32 i = 0; i < r.data[0]; ++i) r.data[r.data[0] - i] = str[i] - '0';
return is;
};
friend ostream &operator << (ostream &os, BigInt r) {
if (r.sign == '-') cout.put(r.sign);
for (INT32 i = r.data[0]; i >= 1; --i) cout << r.data[i];
return os;
};
BigInt operator + (const BigInt &r) const {
BigInt ans;
INT32 x = 0; ans.data[0] = 1;
while (ans.data[0] <= r.data[0] || ans.data[0] <= this->data[0]) {
ans.data[ans.data[0]] = r.data[ans.data[0]] + this->data[ans.data[0]] + x;
x = ans.data[ans.data[0]] / 10;
ans.data[ans.data[0]] %= 10; ++ans.data[0];
};
ans.data[ans.data[0]] = x;
while (ans.data[ans.data[0]] == 0 && ans.data[0] > 1) --ans.data[0];
return ans;
};
BigInt operator - (const BigInt &r) const {
BigInt ans, i, j;
i = *this; j = r;
if (i.data[0] < j.data[0]) {
swap(i, j); ans.sign = '-';
} else if (i.data[0] == j.data[0]) {
int len = i.data[0];
while (len > 0 && i.data[len] == j.data[len]) --len;
if (len > 0 && i.data[len] < j.data[len]) {
swap(i, j); ans.sign = '-';
};
};
ans.data[0] = 1;
while (ans.data[0] <= i.data[0] || ans.data[0] <= j.data[0]) {
if (i.data[ans.data[0]] < j.data[ans.data[0]]) {
i.data[ans.data[0]] += 10;
--i.data[ans.data[0] + 1];
};
ans.data[ans.data[0]] = i.data[ans.data[0]] - j.data[ans.data[0]];
++ans.data[0];
};
while (ans.data[ans.data[0]] == 0 && ans.data[0] > 1) --ans.data[0];
return ans;
};
BigInt operator * (const BigInt &r) const {
BigInt ans;
INT32 x; ans.data[0] = this->data[0] + r.data[0];
for (INT32 i = 1; i <= this->data[0]; ++i) {
x = 0;
for (int j = 1; j <= r.data[0]; ++j) {
ans.data[i + j - 1] = this->data[i] * r.data[j] + x + ans.data[i + j - 1];
x = ans.data[i + j - 1] / 10;
ans.data[i + j - 1] %= 10;
};
ans.data[i + r.data[0]] = x;
};
while (ans.data[ans.data[0]] == 0 && ans.data[0] > 1) --ans.data[0];
return ans;
};
BigInt operator / (const UINT64 &r) const {
BigInt ans;
INT32 x = 0;
for (INT32 i = 1; i <= this->data[0]; ++i) {
ans.data[i] = (x * 10 + this->data[i]) / r;
x = (x * 10 + this->data[i]) % r;
};
ans.data[0] = 1;
while (ans.data[ans.data[0]] == 0 && ans.data[0] < this->data[0]) ++ans.data[0];
return ans;
};
BigInt operator / (BigInt &r) {
BigInt ans;
INT32 sign = compare(this->data, r.data);
if (sign == -1) {ans.data[0] = 1; ans.data[1] = 0; return ans;};
if (sign == 1 || sign == 0) {
BigInt temp; ans.data[0] = this->data[0] - r.data[0] + 1;
for (INT32 i = ans.data[0]; i >= 1; --i) {
memset(temp.data, 0, sizeof temp.data);
numcpy(r.data, temp.data, i);
while (compare(this->data, temp.data) != -1) {++ans.data[i]; subtraction(this->data, temp.data);};
};
};
while (ans.data[ans.data[0]] == 0 && ans.data[0] > 0) --ans.data[0];
return ans;
};
INT32 operator % (const UINT64 &r) const {
BigInt ans;
INT32 x = 0;
for (INT32 i = 1; i <= this->data[0]; ++i) {
ans.data[i] = (x * 10 + this->data[i]) / r;
x = (x * 10 + this->data[i]) % r;
};
ans.data[0] = 1;
while (ans.data[ans.data[0]] == 0 && ans.data[0] < this->data[0]) ++ans.data[0];
return x;
};
BigInt operator % (BigInt &r) {
BigInt ans;
INT32 sign = compare(this->data, r.data);
if (sign == -1) {ans.data[0] = 1; ans.data[1] = 0; return *this;};
if (sign == 1 || sign == 0) {
BigInt temp; ans.data[0] = this->data[0] - r.data[0] + 1;
for (INT32 i = ans.data[0]; i >= 1; --i) {
memset(temp.data, 0, sizeof temp.data);
numcpy(r.data, temp.data, i);
while (compare(this->data, temp.data) != -1) {++ans.data[i]; subtraction(this->data, temp.data);};
};
};
while (ans.data[ans.data[0]] == 0 && ans.data[0] > 0) --ans.data[0];
if (this->data[0] == 0) {this->data[0] = 1; this->data[1] = 0;};
return *this;
};
};
int main(int argc, char *argv[]) {
BigInt n1, n2;
while (cin >> n1 >> n2)
cout << n1 % n2 << endl;
return 0;
};