yaorongjun @ 2024-10-22 20:04:10
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct bigNum{
string num;
bigNum()
{
num.clear();
}
bigNum operator +(const bigNum num2)const{
bigNum ans;
string s1=num;
string s2=num2.num;
int len1=s1.size();
int len2=s2.size();
reverse(s1.begin(),s1.end());
reverse(s2.begin(),s2.end());
int lenn=max(len1,len2);
int x=0;
for(int i=0;i<lenn;i++){
if(i>=len1) s1+='0';
if(i>=len2) s2+='0';
ans.num+=(((s1[i]-'0')+(s2[i]-'0')+x)%10+'0');
x=((s1[i]-'0')+(s2[i]-'0')+x)/10;
}
//cout<<s1<<" "<<s2<<" "<<ans.num<<endl;
if(x==1){
ans.num+=(x+'0');
}
reverse(ans.num.begin(),ans.num.end());
return ans;
}
bigNum operator - (const bigNum num2)const{
bigNum ans;
string s1=num;
string s2=num2.num;
if(s1.size()<s2.size()||(s1.size()==s2.size()&&s1<s2)){
swap(s1,s2);
cout<<"-";
}
int len1=s1.size();
int len2=s2.size();
reverse(s1.begin(),s1.end());
reverse(s2.begin(),s2.end());
int lenn=max(len1,len2);
int x=0;
for(int i=0;i<lenn;i++){
if(i>=len1) s1+='0';
if(i>=len2) s2+='0';
if(s1[i]<s2[i]||s1[i]+x<'0'){
ans.num+=(s1[i]-'0'+10)-(s2[i]-'0')+x+'0';
x=-1;
}else{
ans.num+=(s1[i]-'0')-(s2[i]-'0')+x+'0';
x=0;
}
}
//cout<<s1<<" "<<s2<<" "<<ans.num<<endl;
while(ans.num[lenn-1]=='0'&&lenn>1){
ans.num.erase(lenn-1);
lenn--;
}
reverse(ans.num.begin(),ans.num.end());
return ans;
}
bigNum operator * (const bigNum num2)const{
bigNum ans;
string s1=num;
string s2=num2.num;
int len1=s1.size();
int len2=s2.size();
reverse(s1.begin(),s1.end());
reverse(s2.begin(),s2.end());
int lenn=len1+len2;
while(ans.num.size()<lenn) ans.num+='0';
for(int i=0;i<len1;i++){
int x=0;
for(int j=0;j<len2;j++){
//if(i+j>=ans.num.size()){
// ans.num+=(((s1[i]-'0')*(s2[j]-'0')+x))%10+'0';
//}else{
ans.num[i+j]=(ans.num[i+j]-'0'+((s1[i]-'0')*(s2[j]-'0')+x))%10+'0';
//}
x=((s1[i]-'0')*(s2[j]-'0')+x)/10;
}
ans.num[i+len2]=x+'0';
}
//cout<<s1<<" "<<s2<<" "<<ans.num<<endl;
//cout<<ans.num.size()<<endl;
while(ans.num[lenn-1]=='0'&&lenn>1){
ans.num.erase(lenn-1);
lenn--;
}
reverse(ans.num.begin(),ans.num.end());
return ans;
}
};
int main(){
int n;
cin>>n;
bigNum ans;
ans.num="0";
for(int i=1;i<=n;i++){
bigNum k;
k.num="1";
for(int j=1;j<=i;j++){
bigNum a;
a.num=to_string(j);
k=k*a;
}
ans=ans+k;
}
cout<<ans.num;
return 0;
}
by lihaoran2012 @ 2024-10-24 21:20:34
@yaorongjun 你完全可以少些一堆代码,你可以想想用更简单的方法。
by yaorongjun @ 2024-10-24 21:35:15
@lihaoran2012 练习高精度的时候顺便把减法也写了,大佬能不能帮我看看细节问题