复习大半个月其中,终于回来了。贪心做到一个题要用高精度,所以来这把高精顺便写了,因为
A
C
M
ACM
ACM可以带板子进考场,所以我在网上找了一个板子。
高精度的具体思路就是,因为
c
+
+
c++
c++里
l
o
n
g
l
o
n
g
long\,\,long
longlong也才
19
19
19位,所以当遇到一些大数,比如
1000
1000
1000位,就无法处理了,所以需要使用高精度算法。
高精度具体思路就是把数用数组存下,然后加减乘除都按照我们平时做竖式一样做就行了。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
struct bnum{
int d[maxn],len;
void clean() { while(len > 1 && !d[len-1]) len--; }
bnum() { memset(d, 0, sizeof(d)); len = 1; }
bnum(int num) { *this = num; }
bnum(char* num) { *this = num; }
bnum operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bnum operator = (int num){
char s[20]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bnum operator + (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bnum operator - (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bnum operator * (const bnum& b)const{
int i, j; bnum c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bnum operator / (const bnum& b){
int i, j;
bnum c = *this, a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bnum operator % (const bnum& b){
int i, j;
bnum a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bnum operator += (const bnum& b){
*this = *this + b;
return *this;
}
bool operator <(const bnum& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bnum& b) const{return b < *this;}
bool operator<=(const bnum& b) const{return !(b < *this);}
bool operator>=(const bnum& b) const{return !(*this < b);}
bool operator!=(const bnum& b) const{return b < *this || *this < b;}
bool operator==(const bnum& b) const{return !(b < *this) && !(b > *this);}
string str() const{
char s[maxn]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
};
istream& operator >> (istream& in, bnum& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bnum& x)
{
out << x.str();
return out;
}
这是我在网上找到的板子,利用结构体可以让大数用着跟正常的数一样加减乘除
上题
P1601 A+B Problem(高精)
传送门
用板子
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
struct bnum{
int d[maxn],len;
void clean() { while(len > 1 && !d[len-1]) len--; }
bnum() { memset(d, 0, sizeof(d)); len = 1; }
bnum(int num) { *this = num; }
bnum(char* num) { *this = num; }
bnum operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bnum operator = (int num){
char s[20]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bnum operator + (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bnum operator - (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bnum operator * (const bnum& b)const{
int i, j; bnum c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bnum operator / (const bnum& b){
int i, j;
bnum c = *this, a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bnum operator % (const bnum& b){
int i, j;
bnum a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bnum operator += (const bnum& b){
*this = *this + b;
return *this;
}
bool operator <(const bnum& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bnum& b) const{return b < *this;}
bool operator<=(const bnum& b) const{return !(b < *this);}
bool operator>=(const bnum& b) const{return !(*this < b);}
bool operator!=(const bnum& b) const{return b < *this || *this < b;}
bool operator==(const bnum& b) const{return !(b < *this) && !(b > *this);}
string str() const{
char s[maxn]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
};
istream& operator >> (istream& in, bnum& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bnum& x)
{
out << x.str();
return out;
}
int main(){
bnum a,b;
cin>>a>>b;
cout<<a+b<<endl;
}
P2142 高精度减法
传送门
注意有负数
#include<bits/stdc++.h>
using namespace std;
const int maxn=10100;
struct bnum{
int d[maxn],len;
void clean() { while(len > 1 && !d[len-1]) len--; }
bnum() { memset(d, 0, sizeof(d)); len = 1; }
bnum(int num) { *this = num; }
bnum(char* num) { *this = num; }
bnum operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bnum operator = (int num){
char s[20]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bnum operator + (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bnum operator - (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bnum operator * (const bnum& b)const{
int i, j; bnum c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bnum operator / (const bnum& b){
int i, j;
bnum c = *this, a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bnum operator % (const bnum& b){
int i, j;
bnum a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bnum operator += (const bnum& b){
*this = *this + b;
return *this;
}
bool operator <(const bnum& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bnum& b) const{return b < *this;}
bool operator<=(const bnum& b) const{return !(b < *this);}
bool operator>=(const bnum& b) const{return !(*this < b);}
bool operator!=(const bnum& b) const{return b < *this || *this < b;}
bool operator==(const bnum& b) const{return !(b < *this) && !(b > *this);}
string str() const{
char s[maxn]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
};
istream& operator >> (istream& in, bnum& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bnum& x)
{
out << x.str();
return out;
}
int main(){
bnum a,b;
cin>>a>>b;
if(a<b)cout<<"-"<<b-a<<endl;
else cout<<a-b<<endl;
}
P1303 A*B Problem
#include<bits/stdc++.h>
using namespace std;
const int maxn=10100;
struct bnum{
int d[maxn],len;
void clean() { while(len > 1 && !d[len-1]) len--; }
bnum() { memset(d, 0, sizeof(d)); len = 1; }
bnum(int num) { *this = num; }
bnum(char* num) { *this = num; }
bnum operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bnum operator = (int num){
char s[20]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bnum operator + (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bnum operator - (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bnum operator * (const bnum& b)const{
int i, j; bnum c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bnum operator / (const bnum& b){
int i, j;
bnum c = *this, a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bnum operator % (const bnum& b){
int i, j;
bnum a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bnum operator += (const bnum& b){
*this = *this + b;
return *this;
}
bool operator <(const bnum& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bnum& b) const{return b < *this;}
bool operator<=(const bnum& b) const{return !(b < *this);}
bool operator>=(const bnum& b) const{return !(*this < b);}
bool operator!=(const bnum& b) const{return b < *this || *this < b;}
bool operator==(const bnum& b) const{return !(b < *this) && !(b > *this);}
string str() const{
char s[maxn]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
};
istream& operator >> (istream& in, bnum& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bnum& x)
{
out << x.str();
return out;
}
int main(){
bnum a,b;
cin>>a>>b;
cout<<a*b<<endl;
}
P1480 A/B Problem
#include<bits/stdc++.h>
using namespace std;
const int maxn=10100;
struct bnum{
int d[maxn],len;
void clean() { while(len > 1 && !d[len-1]) len--; }
bnum() { memset(d, 0, sizeof(d)); len = 1; }
bnum(int num) { *this = num; }
bnum(char* num) { *this = num; }
bnum operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bnum operator = (int num){
char s[20]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bnum operator + (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bnum operator - (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bnum operator * (const bnum& b)const{
int i, j; bnum c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bnum operator / (const bnum& b){
int i, j;
bnum c = *this, a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bnum operator % (const bnum& b){
int i, j;
bnum a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bnum operator += (const bnum& b){
*this = *this + b;
return *this;
}
bool operator <(const bnum& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bnum& b) const{return b < *this;}
bool operator<=(const bnum& b) const{return !(b < *this);}
bool operator>=(const bnum& b) const{return !(*this < b);}
bool operator!=(const bnum& b) const{return b < *this || *this < b;}
bool operator==(const bnum& b) const{return !(b < *this) && !(b > *this);}
string str() const{
char s[maxn]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
};
istream& operator >> (istream& in, bnum& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bnum& x)
{
out << x.str();
return out;
}
int main(){
bnum a,b;
cin>>a>>b;
cout<<a/b<<endl;
}
P1009 [NOIP1998 普及组] 阶乘之和
#include<bits/stdc++.h>
using namespace std;
const int maxn=10100;
struct bnum{
int d[maxn],len;
void clean() { while(len > 1 && !d[len-1]) len--; }
bnum() { memset(d, 0, sizeof(d)); len = 1; }
bnum(int num) { *this = num; }
bnum(char* num) { *this = num; }
bnum operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bnum operator = (int num){
char s[20]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bnum operator + (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bnum operator - (const bnum& b){
bnum c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bnum operator * (const bnum& b)const{
int i, j; bnum c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bnum operator / (const bnum& b){
int i, j;
bnum c = *this, a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bnum operator % (const bnum& b){
int i, j;
bnum a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bnum operator += (const bnum& b){
*this = *this + b;
return *this;
}
bool operator <(const bnum& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bnum& b) const{return b < *this;}
bool operator<=(const bnum& b) const{return !(b < *this);}
bool operator>=(const bnum& b) const{return !(*this < b);}
bool operator!=(const bnum& b) const{return b < *this || *this < b;}
bool operator==(const bnum& b) const{return !(b < *this) && !(b > *this);}
string str() const{
char s[maxn]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
};
istream& operator >> (istream& in, bnum& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bnum& x)
{
out << x.str();
return out;
}
int main(){
int n;
cin>>n;
bnum sum;
for(bnum i=1;i<=n;i=i+1){
bnum now=1;
for(bnum j=1;j<=i;j=j+1)now=now*j;
sum+=now;
}
cout<<sum<<endl;
}
总结
板子扒下来 考场直接用就行,这真是我写过最轻松的一次博客哈哈哈哈哈哈。