zengzeyu @ 2024-04-21 12:24:30
#include<bits/stdc++.h>
using namespace std;
string add(string a,string b){
string baik="",baika="";
int ai,bi,pla,carry,add_ab,i = 0;
int alen = a.length();
int blen = b.length();
for(int i = 0;i < 200-alen;i++){
a = "0" + a;
}
for(int i = 0;i < 200-blen;i++){
b = "0" + b;
}
for(int i = 199;i>=0;i--){
ai = a[i] - '0';
bi = b[i] - '0';
add_ab = ai + bi + carry;
pla = add_ab % 10;
/*
if(pla == 0 && carry == 0){
break;
}
*/
baik = to_string(pla) + baik;
carry = (add_ab - pla)/10;
}
//cout << a << " + " << b << " = " << baik << endl;
for(;baik[i] == '0';i++){}
//cout<<i<<endl;
for(;i<=199;i++){
baika = baika + baik[i];
}
baik = baika;
return baik;
}
string mul(string a,string b){
string mul_ab = "",pro_p = "0";
int alen = a.length(),blen = b.length(),carry,pla;
for(int i = blen-1;i >= 0;i--){
carry = 0;
mul_ab = "";
int vb = b[i]-'0';
for(int j = alen-1;j >= 0;j--){
int va = a[j]-'0';
int mab = va * vb + carry;
pla = mab % 10;
mul_ab = to_string(pla)+mul_ab;
carry = (mab -pla) / 10 ;
//carry = 0;
}
if(carry != 0){
mul_ab = char(carry + '0')+mul_ab;
}
for(int j = 1;j < blen - i;j++){
mul_ab = mul_ab + "0";
}
pro_p = add(pro_p, mul_ab);
mul_ab = "";
}
return pro_p;
}
string calcfac(int num){
string res = to_string(num);
if(num == 1){
return res;
}
res = mul(res, calcfac(num-1));
return res;
}
int main(){
int n = 0;
string s = "0";
cin>>n;
//if(n==6) {
// cout << to_string(873) << endl;
// return 0;
//}
//string fac = "1";
for(int i = 1; i <= n; i++){
//fac = mul(fac, to_string(i));
//cout << i << " "<<fac << " " << s << endl;
s = add(calcfac(i),s);
}
cout<<s;
}
by h120316 @ 2024-04-27 10:34:37
建议缩短代码,求关
by xujihong666 @ 2024-04-27 14:18:52
@zengzeyu 样例对了有不一定是都对
by jasonlixuran @ 2024-05-10 18:20:13
AC代码:
#include<bits/stdc++.h>
using namespace std;
int a[1000010]={0,1},b[1000010]={0,1};//a是累加器,b是阶乘,都从2开始
int main()
{
int n;
cin>>n;
for(int i = 2; i<=n; i++)
{
//先计算阶乘
for(int j = 1; j<=100; j++)
{
b[j]*=i;
}
for(int j = 1; j<=100; j++)
{
b[j+1]+=b[j]/10;
b[j]%=10;
}
//再累加
for(int j = 1; j<=100; j++)
{
a[j]+=b[j];
a[j+1]+=a[j]/10;
a[j]%=10;
}
}
//逆序输出
int index=100;
while(a[index]==0)
{
index--;
}
for(int i=index; i>=1; i--)
{
cout<<a[i];
}
}
by Tmc2012 @ 2024-05-10 20:28:50
@zengzeyu 代码没那么难,写短一点!
by zengzeyu @ 2024-05-12 11:17:06
@tangmuchen ok
by xujihong666 @ 2024-08-02 15:35:16
#include<bits/stdc++.h>
using namespace std;
int n,a[1000],s[1000],lem=1;
void cheng(int k){
memset(a,0,sizeof(a));
a[1]=1;
for(int i=1;i<=k;i++){
for(int j=1;j<=lem;j++){
a[j]*=i;
}
for(int j=1;j<=lem;j++){
a[j+1]+=a[j]/10;
a[j]%=10;
if(a[lem+1]!=0)
lem++;
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cheng(i);
for(int j=1;j<=lem;j++){
s[j]+=a[j];
s[j+1]+=s[j]/10;
s[j]%=10;
}
}
while(lem>1){
if(s[lem]!=0)
break;
else
lem--;
}
for(int i=lem;i>=1;i--){
cout<<s[i];
}
return 0;
}
应该能AC****