tanghg @ 2022-06-03 08:12:46
rt。
#include <iostream>
#include <cstring>
using namespace std;
const long long MAX=100;
typedef int ll;
struct Bigint{
ll l,a[MAX];
Bigint(ll x=0){
memset(a,0, sizeof(a));
for (l = 1; x ; ++l) {
a[l]=x%10;
x/=10;
}
l--;
}
int &operator[](ll i){
return a[i];
}
void fla(ll L){
l=L;
for (int i = 1; i <=l ; ++i) {
a[i+1]=a[i]/10;
a[i]%=10;
}
while (!a[l]){
l--;
}
}
void out(){
for (int i = max(1,l); i >=1 ; --i) {
cout<<a[i];
}
}
Bigint operator+(Bigint a,Bigint b){
Bigint c;
ll l= max(a.l,b.l);
for (int i = 1; i <=l ; ++i) {
c[i]+=a[i]+b[i];
}
c.fla(l+1);
return c;
}
Bigint operator*(Bigint a, ll b){
Bigint c;
ll l=a.l;
for (int i = 1; i <=l ; ++i) {
c[i]=a[i]*b;
}
c.fla(l+11);
return c;
}
};
int main() {
Bigint a(0),f(1);
ll n;
cin>>n;
for (int i = 1; i <=n ; ++i) {
f= f * i;
a= a + f;
}
a.out();
return 0;
}
还有一个小问题。
Bigint(ll x=0){
memset(a,0, sizeof(a));
for (l = 1; x ; ++l) {
a[l]=x%10;
x/=10;
}
l--;
}
这一段代码有什么作用呢,为什么要这样初始化? 第一次发帖,有什么问题我会改。
by qfcxxcfrpig_FOROG @ 2022-06-03 08:18:09
@Tanghaoge
题里有这个
【其他说明】
注,《深入浅出基础篇》中使用本题作为例题,但是其数据范围只有 n \le 20n≤20,使用书中的代码无法通过本题。
如果希望通过本题,请继续学习第八章高精度的知识。
by tanghg @ 2022-06-03 08:18:40
@qfcxxcfr 我打的就是高精,第八章
by tanghg @ 2022-06-03 08:57:54
#include <iostream>
using namespace std;
const long long MAX=1000000;
typedef int ll;
struct Bigint{
ll l,a[MAX];
Bigint(ll x=0){
memset(a,0, sizeof(a));
for (l = 1; x ; ++l) {
a[l]=x%10;
x/=10;
}
l--;
}
int &operator[](ll i){
return a[i];
}
void fla(ll L){
l=L;
for (int i = 1; i <=l ; ++i) {
a[i+1]=a[i]/10;
a[i]%=10;
}
while (!a[l]){
l--;
}
}
void out(){
for (int i = max(1,l); i >=1 ; --i) {
cout<<a[i];
}
}
};
Bigint operator+(Bigint a,Bigint b){
Bigint c;
ll l= max(a.l,b.l);
for (int i = 1; i <=l ; ++i) {
c[i]+=a[i]+b[i];
}
c.fla(l+1);
return c;
}
Bigint operator*(Bigint a, ll b){
Bigint c;
ll l=a.l;
for (int i = 1; i <=l ; ++i) {
c[i]=a[i]*b;
}
c.fla(l+11);
return c;
}
int main() {
Bigint a(0),f(1);
ll n;
cin>>n;
for (int i = 1; i <=n ; ++i) {
f=f*i;
a=a+f;
}
a.out();
return 0;
}
发现基本错误了,可还是会有问题
by tanghg @ 2022-06-03 08:59:52
上面没打cstring,可无伤大雅
by tanghg @ 2022-06-03 09:00:10
全wa了
by tanghg @ 2022-06-03 09:00:53
本地能过,ide也能过。
by Super_Supper @ 2022-06-03 09:01:32
@Tanghaoge
暂时不知道
这是构造函数,在定义的时候就能完成初始化
by tanghg @ 2022-06-03 09:01:58
输入5我输出了3?这是为什么
by tanghg @ 2022-06-03 09:02:27
@sb_yyds 谢谢
by tanghg @ 2022-06-03 09:03:12
@sb_yyds 请看第二次的代码