高精度算法(模板)
加法
//高精度加法
//只能是两个正数相加
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
string x,y;
string add(string s1,string s2)
{
string ans;
int l1 = s1.size(),l2 = s2.size();
if( l1 < l2 )
{
for(int i = 1;i <= l2 - l1; i++)
s1 = "0" + s1;
}
else
{
for(int i = 1;i <= l1 - l2; i++)
s2 = "0" + s2;
}
int l = s1.size();
int cf = 0,tmp;
for(int i = l - 1;i >= 0; i--)
{
tmp = s1[i]-'0' + s2[i]-'0' +cf;
cf = tmp/10;
tmp %= 10;
ans = char(tmp + '0') + ans;
}
if( cf != 0 )
ans = char(cf + '0') + ans;
return ans;
}
int main()
{
cin >> x >> y;
cout << add(x,y);
return 0;
}
/*
翻转字符串 reverse(x.begin(),x.end());
*/
减法
//高精度减法
//只能是两个正数相减,而且要大减小
string sub(string s1,string s2)//高精度减法
{
string ans;
int tmp = s1.length()-s2.length();
int cf = 0;
// 处理进退位
for(int i = s2.length()-1;i >= 0;i--)
{
if( s1[ tmp+i ] < s2[i]+cf )
{
ans = char(s1[ tmp+i ]-s2[i]-cf+'0'+10)+ans;
cf = 1;
}
else
{
ans = char(s1[tmp+i]-s2[i]-cf+'0')+ans;
cf = 0;
}
}
for(int i = tmp-1;i >= 0;i--)
{
if( s1[i]-cf >= '0' )
{
ans = char(s1[i]-cf)+ans;
cf = 0;
}
else
{
ans = char(s1[i]-cf+10)+ans;
cf = 1;
}
}
ans.erase(0,ans.find_first_not_of('0'));
//去除结果中多余的前导0
return ans;
}
乘法
//高精度乘法
//只能是两个正数相乘
string mul(string s1,string s2)
{
string ans;
int len1=s1.length();
int len2=s2.length();
if( s1 == "0" || s2 == "0" )
return "0";
string tempstr;
for(int i = len2-1;i >= 0;i--)
{
tempstr = "";
int temp = s2[i]-'0';
int t = 0;
int cf = 0;
if( temp != 0 )
{
for(int j = 1;j <= len2-1-i;j++)
tempstr += "0";
for(int j = len1-1;j >= 0;j--)
{
t = (temp*(s1[j]-'0')+cf)%10;
cf = (temp*(s1[j]-'0')+cf)/10;
tempstr = char(t+'0')+tempstr;
}
if( cf != 0 )
tempstr = char(cf+'0')+tempstr;
}
ans = add(ans,tempstr);
}
ans.erase(0,ans.find_first_not_of('0'));
return ans;
}
#include<iostream>
#include<cstring>
using namespace std;
char a1[50001],b1[50001];
int a[50001],b[50001],i,x,len,j,c[50001];
int main ()
{
cin >>a1 >>b1;//读入两个数
a[0]=strlen(a1);b[0]=strlen(b1);//计算长度
for (i=1;i<=a[0];++i)a[i]=a1[a[0]-i]-'0';//将字符串转换成数字
for (i=1;i<=b[0];++i)b[i]=b1[b[0]-i]-'0';
for (i=1;i<=a[0];++i)for (j=1;j<=b[0];++j)c[i+j-1]+=a[i]*b[j];//按乘法
len=a[0]+b[0]; //原理进行高精乘
for (i=1;i<len;++i)if (c[i]>9){c[i+1]+=c[i]/10;c[i]%=10;}//进位
while (c[len]==0&&len>1)len--;//判断位数
for (i=len;i>=1;--i)cout <<c[i];//输出
return 0;
}
除法
//高精度除法
//两个正数相除,商为quotient,余数为residue
//需要高精度减法和乘法
//compare比较函数:相等返回0,大于返回1,小于返回-1
int compare(string str1,string str2)
{
if( str1.length() > str2.length() )
return 1;
else if( str1.length() < str2.length() )
return -1;
else
return str1.compare(str2);
}
void div(string str1,string str2,string "ient,string &residue)
{
quotient = residue = "";//清空
if( str2 == "0" )//判断除数是否为0
{
quotient = residue = "ERROR";
return;
}
if( str1 == "0" )//判断被除数是否为0
{
quotient = residue = "0";
return;
}
int res = compare(str1,str2);
if( res < 0 )
{
quotient = "0";
residue = str1;
return;
}
else if( res == 0 )
{
quotient = "1";
residue = "0";
return;
}
else
{
int len1 = str1.length();
int len2 = str2.length();
string tempstr;
tempstr.append(str1,0,len2-1);
for(int i = len2-1;i < len1;i++)
{
tempstr = tempstr+str1[i];
tempstr.erase(0,tempstr.find_first_not_of('0'));
if(tempstr.empty())
tempstr = "0";
for(char ch = '9';ch >= '0';ch--)
{
string str,tmp;//试商
str = str+ch;
tmp = mul(str2,str);
if( compare(tmp,tempstr) <= 0 )
{//试商成功
quotient = quotient+ch;
tempstr = sub(tempstr,tmp);
break;
}
}
}
residue = tempstr;
}
quotient.erase(0,quotient.find_first_not_of('0'));
if( quotient.empty() )
quotient="0";
}
结尾
感谢奆佬User_Unauthorized赞助的封装模板
高精度模板
#include<bits/stdc++.h>
using namespace std;
class DividedByZeroException {};
class Int {
private:
vector<char> digits;
bool sign;
void trim();
public:
Int(int);
Int(string&) ;
Int();
Int(const Int&);
Int operator=(const Int& op2);
Int abs() const;
Int pow(int a);
friend Int operator+=(Int&, const Int&);
friend Int operator-=(Int&, const Int&);
friend Int operator*=(Int&, const Int&);
friend Int operator/=(Int&, const Int&) throw(DividedByZeroException);
friend Int operator%=(Int&, const Int&) throw(DividedByZeroException);
friend Int operator+(const Int&, const Int&);
friend Int operator-(const Int&, const Int&);
friend Int operator*(const Int&, const Int&);
friend Int operator/(const Int&, const Int&) throw(DividedByZeroException);
friend Int operator%(const Int&, const Int&) throw(DividedByZeroException);
friend Int operator-(const Int&);
friend Int operator++(Int&);
friend Int operator++(Int&, int);
friend Int operator--(Int&);
friend Int operator--(Int&, int);
friend bool operator>(const Int&, const Int&);
friend bool operator<(const Int&, const Int&);
friend bool operator==(const Int&, const Int&);
friend bool operator!=(const Int&, const Int&);
friend bool operator>=(const Int&, const Int&);
friend bool operator<=(const Int&, const Int&);
friend ostream& operator<<(ostream&, const Int&);
friend istream& operator>>(istream&, Int&);
public:
static const Int ZERO;
static const Int ONE;
static const Int TEN;
};
const Int Int::ZERO = Int(0);
const Int Int::ONE = Int(1);
const Int Int::TEN = Int(10);
Int::Int() {
sign = true;
}
Int::Int(int val) {
if (val >= 0) {
sign = true;
}
else {
sign = false;
val *= (-1);
}
do {
digits.push_back((char)(val % 10));
val /= 10;
} while (val != 0);
}
Int::Int(string& def) {
sign = true;
for (string::reverse_iterator iter = def.rbegin() ; iter < def.rend(); iter++) {
char ch = (*iter);
if (iter == def.rend() - 1) {
if (ch == '+') {
break;
}
if (ch == '-') {
sign = false;
break;
}
}
digits.push_back((char)((*iter) - '0'));
}
trim();
}
void Int::trim() {
vector<char>::reverse_iterator iter = digits.rbegin();
while (!digits.empty() && (*iter) == 0) {
digits.pop_back();
iter = digits.rbegin();
}
if (digits.size() == 0) {
sign = true;
digits.push_back(0);
}
}
Int::Int(const Int& op2) {
sign = op2.sign;
digits = op2.digits;
}
Int Int::operator=(const Int& op2) {
digits = op2.digits;
sign = op2.sign;
return (*this);
}
Int Int::abs() const {
if (sign) {
return *this;
}
else {
return -(*this);
}
}
Int Int::pow(int a) {
Int res(1);
for (int i = 0; i < a; i++) {
res *= (*this);
}
return res;
}
Int operator+=(Int& op1, const Int& op2) {
if (op1.sign == op2.sign) {
vector<char>::iterator iter1;
vector<char>::const_iterator iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin();
char to_add = 0;
while (iter1 != op1.digits.end() && iter2 != op2.digits.end()) {
(*iter1) = (*iter1) + (*iter2) + to_add;
to_add = ((*iter1) > 9);
(*iter1) = (*iter1) % 10;
iter1++;
iter2++;
}
while (iter1 != op1.digits.end()) {
(*iter1) = (*iter1) + to_add;
to_add = ((*iter1) > 9);
(*iter1) %= 10;
iter1++;
}
while (iter2 != op2.digits.end()) {
char val = (*iter2) + to_add;
to_add = (val > 9) ;
val %= 10;
op1.digits.push_back(val);
iter2++;
}
if (to_add != 0) {
op1.digits.push_back(to_add);
}
return op1;
}
else {
if (op1.sign) {
return op1 -= (-op2);
}
else {
return op1 = op2 - (-op1);
}
}
}
Int operator-=(Int& op1, const Int& op2) {
if (op1.sign == op2.sign) {
if (op1.sign) {
if (op1 < op2) {
return op1 = -(op2 - op1);
}
}
else {
if (-op1 > -op2) {
return op1 = -((-op1) - (-op2));
}
else {
return op1 = (-op2) - (-op1);
}
}
vector<char>::iterator iter1;
vector<char>::const_iterator iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin();
char to_substract = 0;
while (iter1 != op1.digits.end() && iter2 != op2.digits.end()) {
(*iter1) = (*iter1) - (*iter2) - to_substract;
to_substract = 0;
if ((*iter1) < 0) {
to_substract = 1;
(*iter1) += 10;
}
iter1++;
iter2++;
}
while (iter1 != op1.digits.end()) {
(*iter1) = (*iter1) - to_substract;
to_substract = 0;
if ((*iter1) < 0) {
to_substract = 1;
(*iter1) += 10;
}
else {
break;
}
iter1++;
}
op1.trim();
return op1;
}
else {
if (op1 > Int::ZERO) {
return op1 += (-op2);
}
else {
return op1 = -(op2 + (-op1));
}
}
}
Int operator*=(Int& op1, const Int& op2) {
Int result(0);
if (op1 == Int::ZERO || op2 == Int::ZERO) {
result = Int::ZERO;
}
else {
vector<char>::const_iterator iter2 = op2.digits.begin();
while (iter2 != op2.digits.end()) {
if (*iter2 != 0) {
deque<char> temp(op1.digits.begin(), op1.digits.end());
char to_add = 0;
deque<char>::iterator iter1 = temp.begin();
while (iter1 != temp.end()) {
(*iter1) *= (*iter2);
(*iter1) += to_add;
to_add = (*iter1) / 10;
(*iter1) %= 10;
iter1++;
}
if (to_add != 0) {
temp.push_back(to_add);
}
int num_of_zeros = iter2 - op2.digits.begin();
while (num_of_zeros--) {
temp.push_front(0);
}
Int temp2;
temp2.digits.insert(temp2.digits.end(), temp.begin(), temp.end());
temp2.trim();
result = result + temp2;
}
iter2++;
}
result.sign = ((op1.sign && op2.sign) || (!op1.sign && !op2.sign));
}
op1 = result;
return op1;
}
Int operator/=(Int& op1, const Int& op2) throw(DividedByZeroException) {
if (op2 == Int::ZERO) {
throw DividedByZeroException();
}
Int t1 = op1.abs(), t2 = op2.abs();
if (t1 < t2) {
op1 = Int::ZERO;
return op1;
}
deque<char> temp;
vector<char>::reverse_iterator iter = t1.digits.rbegin();
Int temp2(0);
while (iter != t1.digits.rend()) {
temp2 = temp2 * Int::TEN + Int((int)(*iter));
char s = 0;
while (temp2 >= t2) {
temp2 = temp2 - t2;
s = s + 1;
}
temp.push_front(s);
iter++;
}
op1.digits.clear();
op1.digits.insert(op1.digits.end(), temp.begin(), temp.end());
op1.trim();
op1.sign = ((op1.sign && op2.sign) || (!op1.sign && !op2.sign));
return op1;
}
Int operator%=(Int& op1, const Int& op2) throw(DividedByZeroException) {
return op1 -= ((op1 / op2) * op2);
}
Int operator+(const Int& op1, const Int& op2) {
Int temp(op1);
temp += op2;
return temp;
}
Int operator-(const Int& op1, const Int& op2) {
Int temp(op1);
temp -= op2;
return temp;
}
Int operator*(const Int& op1, const Int& op2) {
Int temp(op1);
temp *= op2;
return temp;
}
Int operator/(const Int& op1, const Int& op2) throw(DividedByZeroException) {
Int temp(op1);
temp /= op2;
return temp;
}
Int operator%(const Int& op1, const Int& op2) throw(DividedByZeroException) {
Int temp(op1);
temp %= op2;
return temp;
}
Int operator-(const Int& op) {
Int temp = Int(op);
temp.sign = !temp.sign;
return temp;
}
Int operator++(Int& op) {
op += Int::ONE;
return op;
}
Int operator++(Int& op, int x) {
Int temp(op);
++op;
return temp;
}
Int operator--(Int& op) {
op -= Int::ONE;
return op;
}
Int operator--(Int& op, int x) {
Int temp(op);
--op;
return temp;
}
bool operator<(const Int& op1, const Int& op2) {
if (op1.sign != op2.sign) {
return !op1.sign;
}
else {
if (op1.digits.size() != op2.digits.size())
return (op1.sign && op1.digits.size() < op2.digits.size())
|| (!op1.sign && op1.digits.size() > op2.digits.size());
vector<char>::const_reverse_iterator iter1, iter2;
iter1 = op1.digits.rbegin();
iter2 = op2.digits.rbegin();
while (iter1 != op1.digits.rend()) {
if (op1.sign && *iter1 < *iter2) {
return true;
}
if (op1.sign && *iter1 > *iter2) {
return false;
}
if (!op1.sign && *iter1 > *iter2) {
return true;
}
if (!op1.sign && *iter1 < *iter2) {
return false;
}
iter1++;
iter2++;
}
return false;
}
}
bool operator==(const Int& op1, const Int& op2) {
if (op1.sign != op2.sign || op1.digits.size() != op2.digits.size()) {
return false;
}
vector<char>::const_iterator iter1, iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin();
while (iter1 != op1.digits.end()) {
if (*iter1 != *iter2) {
return false;
}
iter1++;
iter2++;
}
return true;
}
bool operator!=(const Int& op1, const Int& op2) {
return !(op1 == op2);
}
bool operator>=(const Int& op1, const Int& op2) {
return (op1 > op2) || (op1 == op2);
}
bool operator<=(const Int& op1, const Int& op2) {
return (op1 < op2) || (op1 == op2);
}
bool operator>(const Int& op1, const Int& op2) {
return !(op1 <= op2);
}
ostream& operator<<(ostream& stream, const Int& val) {
if (!val.sign) {
stream << "-";
}
for (vector<char>::const_reverse_iterator iter = val.digits.rbegin(); iter != val.digits.rend() ; iter++) {
stream << (char)((*iter) + '0');
}
return stream;
}
istream& operator>>(istream& stream, Int& val) {
string str;
stream >> str;
val = Int(str);
return stream;
}
Int a,b;
int main()
{
cin >> a >> b;
cout << a+b << endl << a-b<<endl<<a*b<<endl<<a/b<<endl<<a%b;
}