pascal为什么只有90分,数组没有开小啊

P1255 数楼梯

邓晓蓝 @ 2016-03-15 12:57:57

type arr=array[0..10000] of longint;
var n,i:longint;
    a,b,c:arr;
procedure jf(var a,b:arr;var c:arr);
var i:longint;
begin
  for i:=0 to 1000 do
    c[i]:=0;
  if a[0]>b[0] then c[0]:=a[0] else c[0]:=b[0];
  for i:=1 to c[0] do
    begin
      c[i]:=c[i]+a[i]+b[i];
      c[i+1]:=c[i] div 10;
      c[i]:=c[i] mod 10;
    end;
  if c[c[0]+1]<>0 then c[0]:=c[0]+1;
end;
begin
  readln(n);
  a[0]:=1;a[1]:=1;b[0]:=1;b[1]:=2;
  if n=1 then writeln(1) else
  if n=2 then writeln(1) else
  begin
  for i:=3 to n do
    begin
      jf(a,b,c);
      a:=b;b:=c;
    end;
  for i:=b[0] downto 1 do write(b[i]);
  end;
end.
//求大牛帮忙。

by IandYOU @ 2016-05-23 20:32:39

有没有考虑特殊情况


by 蒟蒻pks @ 2016-08-27 19:55:07

有可能是0 我也被坑了


by 杨志远2005 @ 2016-09-24 09:39:50

ar i,n:longint;
a:array[1..5001] of ansistring;
procedure gid(st1,st2:ansistring;var st3:ansistring);
var a,b,c:array[1..5001] of longint;i,j,ln3,ln1,ln2:longint;
begin
 fillchar(a,sizeof(a),0);
 fillchar(b,sizeof(b),0);
 fillchar(c,sizeof(c),0);
 ln1:=length(st1);
 ln2:=length(st2);
 j:=0;
 for i:=ln1 downto 1 do
 begin
    inc(j);
    a[j]:=ord(st1[i])-ord('0');
 end;
 j:=0;
 for i:=ln2 downto 1 do
 begin
    inc(j);
    b[j]:=ord(st2[i])-ord('0');
 end;
  if ln1>ln2 then ln3:=ln1 else ln3:=ln2;
 for i:=1 to ln3 do
    begin
      c[i]:=a[i]+b[i]+c[i];
      c[i+1]:=c[i] div 10;
      c[i]:=c[i] mod 10;
    end;
 if c[ln3+1]<>0 then inc(ln3);
 for i:=ln3 downto 1 do
  st3:=st3+chr(c[i]+ord('0'));
end;//高静度函数
begin
 read(n);
 if n=0 then begin write(0);exit;end;
 a[1]:='1';
 a[2]:='2';
 for i:=3 to n do
    gid(a[i-1],a[i-2],a[i]);
    writeln(a[n]);
end.

by hellomath @ 2016-12-12 13:12:21

why?

#include <iostream>
#include <cstring>
#include <sstream>
using namespace std;
string add(string a_str, string b_str)
{
    int a[a_str.length()], b[b_str.length()];
    for (int i=0; i<a_str.length(); i++) a[i] = a_str[a_str.length()-1-i] - '0';
    for (int i=0; i<b_str.length(); i++) b[i] = b_str[b_str.length()-1-i] - '0';
    int maxSize = max(a_str.length(), b_str.length())+1;
    int c[maxSize];
    memset(c, 0, sizeof(c));
    for (int i=0; i<maxSize; i++)
    {
        if (i<a_str.length() && i<b_str.length())
            c[i] += a[i] + b[i];
        else if (i>=a_str.length() && i<b_str.length())
            c[i] += b[i];
        else if (i<a_str.length() && i>=b_str.length())
            c[i] += a[i];
        if (c[i]>=10)
        {
            c[i+1]++;
            c[i] %= 10;
        }
    }
    ostringstream oss;
    for (int i=0; i<maxSize; i++)
        oss<<c[maxSize-1-i];
    string s = oss.str();
    while (s[0]=='0' && s.length()>1) s.erase(0, 1);
    return s;
}
int main()
{
    int n;
    cin>>n;
    if (n == 0) cout<<"0"<<endl;
    else if (n == 1) cout<<"1"<<endl;
    else if (n == 2) cout<<"2"<<endl;
    string a[n+1];
    a[1] = "1";
    a[2] = "2";
    for (int i=3; i<=n; i++)
        a[i] = add(a[i-1], a[i-2]);
    cout<<a[n]<<endl;
    return 0;
}

|