只有90分,错误216

P1255 数楼梯

kczno1 @ 2015-09-13 13:58:15

type gjd=array[0..2001]of longint;
var
  n,i:longint;
  f:array[1..5000]of gjd;
function add(a,b:gjd):gjd;
var i:longint;
begin
  for i:=1 to b[0] do
  begin
    a[i]:=a[i]+b[i];
    inc(a[i+1],a[i] div 10);
    a[i]:=a[i] mod 10;
  end;
  if b[0]>a[0] then a[0]:=b[0];
  for i:=b[0]+1 to a[0] do
  begin
    inc(a[i+1],a[i] div 10);
    a[i]:=a[i] mod 10;
  end;
  if a[a[0]+1]>0 then inc(a[0]);
  exit(a);
end;
begin
  readln(n);
  f[1,0]:=1; f[2,0]:=1;
  f[1,1]:=1; f[2,1]:=2;
  for i:=3 to n do
   f[i]:=add(f[i-1],f[i-2]);
  for i:=f[n,0] downto 1 do
   write(f[n,i]);
end.

by controlf2 @ 2015-11-01 19:45:23

216错误是存取非法,我也出过,定义更大的变量就好了


by kczno1 @ 2015-11-05 21:55:18

@[url=/space/show?uid=10793]controlf2[/url] 请问你说的是哪个变量要改大点


by kczno1 @ 2015-11-05 21:55:50

话说我5000都试过了。。


by 汪力宾 @ 2016-01-10 18:09:36

那试试看1000000


by 汪力宾 @ 2016-01-10 18:10:07

反正不会超空间


by sunisn @ 2023-08-15 17:16:17

@kczno1 试试看5005


by sunisn @ 2023-08-15 17:18:09

#include <iostream>
#include <cstring>
using namespace std;
struct BIG
{
    int len, num[5005];
    BIG() {len = 1, memset(num, 0, sizeof(num));}
    void set(int n)
    {
        len = 0, memset(num, 0, sizeof(num));
        while(n > 0) num[++len] = n % 10, n /= 10;
        if(len == 0) len = 1;
        return ;
    }
    void set(string s)
    {
        len = s.size(), memset(num, 0, sizeof(num));
        for(int i = 1; i <= len; i++)
            num[i] = s[len - i] - '0';
        return ;
    }
    void print()
    {
        for(int i = len; i >= 1; i--)
            cout << num[i];
        cout << endl;
        return ;
    }
};
BIG operator+(BIG a, BIG b)
{
    BIG c;
    c.len = max(a.len, b.len);
    int u = 0;
    for(int i = 1; i <= c.len; i++)
    {
        int t = a.num[i] + b.num[i] + u;
        c.num[i] = t % 10, u = t / 10;
    }
    if(u > 0) c.num[++c.len] = u;
    return c;
}
int n;
BIG f[5005];
int main()
{
    cin >> n, f[0].set(1), f[1].set(1);
    for(int i = 2; i <= n; i++)
        f[i] = f[i - 1] + f[i - 2];
    f[n].print();

    return 0;
}

AC代码


|