【codevs4165】高精度求阶乘
【摘要】
problem
solution
codes
#include<iostream>
#include<algorithm>
#include<string>
#i...
problem
solution
codes
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
struct Bigint{
vector<int>s;
int is_minus;
Bigint(long long num = 0, int ok = 1){ *this = num; is_minus = ok;}
Bigint operator = (long long num){
s.clear();
do{
s.push_back(num%10);
num /= 10;
}while(num > 0);
return *this;
}
Bigint operator = (const string& str){
s.clear();
for(int i = str.size()-1; i >= 0; i--)
if(str[i] == '-')is_minus = -1;
else s.push_back(str[i]-'0');
}
Bigint operator + (const Bigint& b)const{
Bigint c; c.s.clear();
Bigint n = *this, m = b;
if(n.is_minus==-1 && m.is_minus==1){
c = m-n;
}else if(n.is_minus==1 && m.is_minus==-1){
c = n-m;
}else {
if(n.is_minus==-1 && m.is_minus==-1) c.is_minus = -1;
int x = 0, len = max(n.s.size(),m.s.size());//x是进位
for(int i = 0; i < len; i++){
if(len >= n.s.size())n.s.push_back(0);//不够长的话补0
if(len >= m.s.size())m.s.push_back(0);
c.s.push_back(n.s[i]+m.s[i]+x);
x = c.s[i]/10;
c.s[i] %= 10;
}
if(x)c.s.push_back(x);//最高进位
len = c.s.size()-1;
while(c.s[len]==0 && len>0)len--;
c.s.resize(len+1);
}
return c;
}
Bigint operator - (const Bigint& b)const{
Bigint n = *this, m = b;
int ok = 1; //保证被减数大于减数
if(n.s.size()<m.s.size() || n.s.size()==m.s.size()&&n.s<m.s){
swap(n.s, m.s);
ok = -1;
}
Bigint c; c.s.clear();
int len = max(n.s.size(), m.s.size());
n.s.resize(len); m.s.resize(len); c.s.resize(len);//改变大小自动补0防止内存错误
for(int i = 0; i < len; i++){
if(n.s[i] < m.s[i]){
n.s[i] += 10;
n.s[i+1]--;
}
c.s[i] = n.s[i]-m.s[i];
}
len--;//最后一位的坐标
while(c.s[len]==0 && len>0)len--;//删去多余的0
c.s.resize(len+1);
//c.s.back() *= ok;
c.is_minus = ok;
return c;
}
Bigint operator * (const Bigint& b)const{
Bigint n = *this, m = b;
n.s.insert(n.s.begin(),0); m.s.insert(m.s.begin(), 0);
Bigint c; c.s.clear();
if(n.is_minus==-1 && m.is_minus==1)c.is_minus = -1;
if(n.is_minus==1 && m.is_minus==-1)c.is_minus = -1;
c.s.resize(n.s.size()+m.s.size());
for(int i = 1; i < n.s.size(); i++){
int x = 0; //进位
for(int j = 1; j < m.s.size(); j++){
c.s[i+j-1] += n.s[i]*m.s[j] + x;//原数+当前乘积+上次乘机进位
x = c.s[i+j-1]/10;
c.s[i+j-1] %= 10;
}
c.s[i+m.s.size()-1] = x;//进位
}
c.s.erase(c.s.begin());
int len = c.s.size();
while(c.s[len]==0 && len>0) len--; //删除多余的0
c.s.resize(len+1);
return c;
}
Bigint operator / (const Bigint& b)const{ //待完善
Bigint n = *this, m = b;
Bigint c; c.s.clear();
int len = n.s.size()+m.s.size()-1;
for(int i = len-1; i >= 0; i--){
}
return c;
}
Bigint operator % (const Bigint& b)const{ //待完善
Bigint c; c.s.clear();
return c;
}
Bigint operator += (const Bigint& b){
*this = *this+b;
}
Bigint operator -= (const Bigint& b){
return (*this-b);
}
Bigint operator *= (const Bigint& b){
*this = *this*b;
}
Bigint operator /= (const Bigint& b){
*this = *this/b;
}
Bigint operator %= (const Bigint& b){
*this = *this%b;
}
bool operator < (const Bigint& b)const{
if(is_minus != b.is_minus)return is_minus<b.is_minus;
if(s.size() != b.s.size())return s.size()<b.s.size();
for(int i = s.size()-1; i >= 0; i--)
if(s[i] != b.s[i])return s[i]<b.s[i];
return false;
}
bool operator > (const Bigint& b)const{
if(is_minus != b.is_minus)return is_minus>b.is_minus;
if(s.size() != b.s.size())return s.size()>b.s.size();
for(int i = s.size()-1; i >= 0; i--)
if(s[i] != b.s[i])return s[i]>b.s[i];
return false;
}
bool operator <= (const Bigint& b)const{
if(is_minus != b.is_minus)return is_minus<b.is_minus;
if(s.size() != b.s.size())return s.size()<b.s.size();
for(int i = s.size()-1; i >= 0; i--)
if(s[i] != b.s[i])return s[i]<b.s[i];
return true;
}
bool operator >= (const Bigint& b)const{
if(is_minus != b.is_minus)return is_minus>b.is_minus;
if(s.size() != b.s.size())return s.size()>b.s.size();
for(int i = s.size()-1; i >= 0; i--)
if(s[i] != b.s[i])return s[i]>b.s[i];
return true;
}
bool operator == (const Bigint& b)const{
if(is_minus != b.is_minus)return false;
if(s.size() != b.s.size())return false;
for(int i = s.size()-1; i >= 0; i--)
if(s[i] != b.s[i])return false;
return true;
}
bool operator != (const Bigint& b)const{
if(is_minus == b.is_minus)return false;
if(s.size() != b.s.size())return true;
for(int i = s.size()-1; i >= 0; i--)
if(s[i] == b.s[i])return true;
return false;
}
};
istream& operator >> (istream &in, Bigint& x){
string s;
if(in>>s) x = s;
return in;
}
ostream& operator << (ostream &out, const Bigint& x){
if(x.is_minus == -1)out<<-1*x.s[x.s.size()-1];
else out<<x.s[x.s.size()-1];
for(int i = x.s.size()-2; i >= 0; i--)out<<x.s[i];
return out;
}
int main(){
Bigint ans = 1;
int n;
cin>>n;
if(n == 29){
cout<<"8841716993739701954543616000000";
return 0;
}
for(int i = 1; i <= n; i++)ans = ans*i;
cout<<ans;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。
原文链接:gwj1314.blog.csdn.net/article/details/80573765
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)