Shrimp123 @ 2024-02-07 12:40:07
#include<bits/stdc++.h>
using namespace std;
string cheng(string,string);
string jia(string,string);
string jiecheng(int);
string jia(string a,string b) {
if(a.length()<b.length())return cheng(b,a);
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int lena=a.length();
int lenb=b.length();
string sum;
int t=0;
int k=0;
for(int i=0; i<lenb||i<lena; i++) {
k=1;
if(i<lena)k+=a[i]-'0';
if(i<lenb)k+=b[i]-'0';
sum+=(k%10+t)+'0';
t=k/10;
}
if(t>0)sum+=t+'0';
reverse(sum.begin(),sum.end());
return sum;
}
string cheng(string a,string b) {
if(a.length()<b.length())return cheng(b,a);
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int lena=a.length();
int lenb=b.length();
string sum="0";
int t=0;
int k=0;
vector<string> tol(lena * lenb, "0");
for(int i=0; i<lena; i++) {
for(int j=0; j<lenb; j++) {
k=a[i]*b[j];
tol[i]+=(k%10+t)+'0';
t=k/10;
}
if(t>0)tol[i]+=t+'0';
reverse(tol[i].begin(),tol[i].end());
for(int j=0; j<i; j++)tol[i]+='0';
sum=jia(sum,tol[i]);
}
return sum;
}
string jiecheng(int n) {
if(n==1)return "1";
string a;
a=to_string(n);
//cout<<a<<" ";
return cheng(a,jiecheng(n-1));
}
string f(int n) {
string sum;
for(int i=1; i<=n; i++) {
sum=jia(sum,jiecheng(i));
}
return sum;
}
int main()
{
int n;
cin>>n;
cout<<f(n);
return 0;
}
by YoungL @ 2024-02-07 13:52:30
jia函数和cheng函数递归了
by YoungL @ 2024-02-07 13:53:27
if(a.length()<b.length())return cheng(b,a);
这一句jia和cheng都有,不是很理解为什么加法要调用乘法
by YoungL @ 2024-02-07 13:55:03
你想调用的是jia吧,但其实swap就行了
by wfls_zhuyiwen @ 2024-02-07 13:58:44
还有,string不能和char直接相加,你的加和乘都出现了这个错误,肯定会运行错误
by Shrimp123 @ 2024-02-07 16:17:31
@YoungL 对对,这个是笔误,但是改了还是不对呀
by Shrimp123 @ 2024-02-07 16:19:23
@zhuyiwen10 为什么呢,我单独这样试好像没问题,
#include<iostream>
using namespace std;
int main()
{
string a="Hell";
a+='o';
cout<<a;
return 0;
}
这个就可以输出Hello呀
by YoungL @ 2024-02-07 16:43:13
@zhuyiwen10 这个是没有问题的
by YoungL @ 2024-02-07 16:43:35
@Shrimp123 改了之后应该能解决RE的问题,但是如果答案不对那就是算法有问题了
by remake1958 @ 2024-02-10 00:49:46
k=a[i]b[j];这个是不是有问题,应该要减个‘0’吧 k=a[i]b[j]; tol[i]+=(k%10+t)+'0'; t=k/10;其实你这一段在逻辑上应该都有问题,每一位相乘之后是要对应i+j和i+j+1的