题解

132 条题解

  • -1
    @ 2015-08-22 21:25:30

    #include <iostream>
    //#include <fstream>
    using namespace std;
    int Double(int *s,int n=100)
    {
    for(int i=0;i<=n;i++)s[i]+=s[i];
    for(int i=0;i<=n;i++) if(s[i]>9)s[i+1]+=s[i]/10,s[i]%=10;
    return 0;
    }
    int add2(int *s,int n=100)
    {
    s[0]+=2;
    for(int i=0;(i<=n)&&(s[i]>9);i++) s[i+1]+=s[i]/10,s[i]%=10;
    return 0;
    }
    int hn(int num,int *s)
    {
    if(num==1) s[0]=2;
    else hn(num-1,s),Double(s),add2(s);
    return 0;
    }
    int main()
    {
    //ifstream cin("p1345.in",ios::in);
    //ofstream cout("p1345.out",ios::out);
    int num;
    cin>>num;
    int s[111]={0};
    hn(num,s);
    int l;
    for(l=110;s[l]==0;l--);
    for(int i=l;i>=0;i--)cout<<s[i];
    //cin.close();
    //cout.close();
    return 0;
    }

  • -1
    @ 2015-08-18 14:27:47

    水题一个
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int ans[1000],b[1000];
    int main()
    {
    //freopen("hanoi.in","r",stdin);
    //freopen("hanoi.out","w",stdout);
    int n,la,i,j;
    scanf("%d",&n);
    memset(ans,0,sizeof(ans));
    ans[1]=2;
    la=1;
    for(i=2;i<=n;i++)
    {
    memset(b,0,sizeof(b));
    for(j=1;j<=la;j++)
    {
    b[j+1]=(b[j]+ans[j]*2)/10;
    b[j]=(b[j]+ans[j]*2)%10;
    }
    b[1]+=2;
    j=1;
    while(b[j]>=10)
    {
    b[j+1]+=b[j]/10;
    b[j]%=10;
    j++;
    }
    la+=2;
    while(b[la]==0)
    la--;
    for(j=la;j>=1;j--)
    ans[j]=b[j];
    }
    for(i=la;i>=1;i--)
    printf("%d",ans[i]);
    printf("\n");
    return 0;
    }

  • -1
    @ 2015-08-15 21:21:21

    ###还要用到高精度
    P1354Hanoi双塔问题Accepted
    记录信息
    评测状态 Accepted
    题目 P1354 Hanoi双塔问题
    递交时间 2015-08-15 21:20:13
    代码语言 C++
    评测机 VijosEx
    消耗时间 81 ms
    消耗内存 516 KiB
    评测时间 2015-08-15 21:20:14
    评测结果
    编译成功

    测试数据 #0: Accepted, time = 15 ms, mem = 516 KiB, score = 10
    测试数据 #1: Accepted, time = 7 ms, mem = 516 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 516 KiB, score = 10
    测试数据 #3: Accepted, time = 15 ms, mem = 516 KiB, score = 10
    测试数据 #4: Accepted, time = 15 ms, mem = 516 KiB, score = 10
    测试数据 #5: Accepted, time = 0 ms, mem = 516 KiB, score = 10
    测试数据 #6: Accepted, time = 1 ms, mem = 516 KiB, score = 10
    测试数据 #7: Accepted, time = 13 ms, mem = 516 KiB, score = 10
    测试数据 #8: Accepted, time = 15 ms, mem = 516 KiB, score = 10
    测试数据 #9: Accepted, time = 0 ms, mem = 512 KiB, score = 10
    Accepted, time = 81 ms, mem = 516 KiB, score = 100
    代码
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    int ans[50];
    long long work(int n)
    {
    if(n==1)return 2;
    return (long long)2*work(n-1)+2;
    }
    int main()
    {
    int n;
    scanf("%d",&n);
    int maxnow=1;
    for(int i=1;i<=n;i++)
    {
    ans[1]+=1;
    int in=0;
    int j;
    for(j=1;j<=maxnow||in;j++)
    {
    ans[j]=2*ans[j]+in;
    in=ans[j]/10;
    ans[j]%=10;

    }
    if(j-1>maxnow)
    maxnow=j-1;
    }

    for(int i=maxnow;i>0;i--)
    printf("%d",ans[i]);
    }

  • -1
    @ 2014-12-14 10:32:28

    var
    a:ARRAY[1..61] OF -2..20;
    i,j,k,h,m,n:longint;
    begin
    readln(n);
    a[1]:=2;
    M:=1;
    for J:=1 to n do
    BEGIN
    K:=0;
    FOR I:=1 TO M DO
    BEGIN
    A[I]:=A[I]*2+K;
    K:=A[I] DIV 10;
    A[I]:=A[I] MOD 10;
    END;
    IF K>0 THEN BEGIN INC(M);
    A[M]:=K;
    END;
    END;
    I:=1;
    DEC(A[I],2);
    WHILE A[I]<0 DO
    BEGIN
    INC(A[I],10);
    INC(I);
    DEC(A[I],1);
    END;
    WHILE A[M]=0 DO DEC(M);
    FOR I:=M DOWNTO 1 DO WRITE(A[I]);
    END.

  • -1
    @ 2014-12-06 16:57:24

    评测状态 Accepted
    题目 P1354 Hanoi双塔问题
    递交时间 2014-12-06 16:56:52
    代码语言 Pascal
    评测机 上海红茶馆
    消耗时间 0 ms
    消耗内存 816 KiB
    评测时间 2014-12-06 16:56:52
    评测结果
    编译成功

    测试数据 #0: Accepted, time = 0 ms, mem = 812 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 816 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 812 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 816 KiB, score = 10
    测试数据 #4: Accepted, time = 0 ms, mem = 816 KiB, score = 10
    测试数据 #5: Accepted, time = 0 ms, mem = 816 KiB, score = 10
    测试数据 #6: Accepted, time = 0 ms, mem = 812 KiB, score = 10
    测试数据 #7: Accepted, time = 0 ms, mem = 816 KiB, score = 10
    测试数据 #8: Accepted, time = 0 ms, mem = 812 KiB, score = 10
    测试数据 #9: Accepted, time = 0 ms, mem = 812 KiB, score = 10
    Accepted, time = 0 ms, mem = 816 KiB, score = 100
    代码
    var
    st1,st2:string;
    a,b,c,d:longint;
    procedure jian;
    var a,b,c,d,len1,len2:longint;
    s2:string;
    begin
    len1:=length(st1);
    s2:='2';
    len2:=length(s2);
    a:=0;
    while len2<>0 do
    begin
    b:=ord(st1[len1])-48;
    c:=ord(st2[len2])-48;
    b:=b-c-a;
    if b<0 then
    begin
    b:=b+10;
    a:=1;
    end
    else
    a:=0;
    st1[len1]:=chr(b+48);
    dec(len1);
    dec(len2);
    end;
    a:=1;
    while st1[a]='0' do inc(a);
    if a=length(st1)+1 then
    begin
    write('0');
    exit;
    end;
    for b:=a to length(st1) do
    begin
    write(st1[b]);
    c:=1;
    end;
    end;
    procedure cheng;
    var
    a,b:array[1..250] of integer;
    c:array[1..500] of integer;
    i,j,len,len1,len2,t:integer;
    fh1,fh2:char;
    begin
    len1:=length(st1);
    len2:=length(st2);
    fillchar(c,sizeof(a),0);
    for i:=1 to len1 do
    a[i]:=ord(st1[len1-i+1])-48;
    for i:=1 to len2 do
    b[i]:=ord(st2[len2-i+1])-48;
    for i:=1 to len1 do
    begin
    t:=i;
    for j:=1 to len2 do
    begin
    c[t]:=c[t]+a[i]*b[j];
    c[t+1]:=c[t] div 10+c[t+1];
    c[t]:=c[t] mod 10;
    inc(t);
    end;
    end;
    while c[t]=0 do t:=t-1; st1:='';
    for i:=t downto 1 do st1:=st1+chr(c[i]+48);
    end;

    begin
    st1:='2';
    st2:=st1;
    readln(a);
    for b:=1 to a do cheng;
    jian;
    end.

  • -1
    @ 2014-10-31 20:02:09

    数学自己解吧。一个数列的通项公式而已。
    program hanoi;
    var n:integer;
    a:array[1..10000] of longint;
    i,x,m,c,line,num:longint;
    op:boolean;
    begin
    //assign(input,'hanoi.in'); reset(input);
    //assign(output,'hanoi.out'); rewrite(output);
    op:=false;
    read(n);
    a[1]:=1;
    m:=1;
    x:=0;
    line:=1;
    if n=1 then
    write('2')
    else
    begin
    for i:=1 to n do
    begin
    for num:=1 to line do
    a[num]:=a[num]*2;

    for num:=1 to line do
    if a[num]>=10 then
    begin x:=a[num] div 10; a[num]:=a[num] mod 10; a[num+1]:=a[num+1]+x; end;

    if a[num+1]<>0 then
    line:=line+1;
    end;

    for c:=10000 downto 1 do
    begin
    if (a[c]<>0) and (op=false) then
    begin op:=true; line:=c; end;
    if op=true then
    break;
    end;

    a[1]:=a[1]-1;
    while a[m]=-1 do
    begin
    a[m]:=9;
    m:=m+1;
    a[m]:=a[m]-1;
    end;

    m:=0;
    for i:=1 to line do
    a[i]:=a[i]*2;

    for i:=1 to line do
    if a[i]>=10 then
    begin x:=a[i] div 10; a[i]:=a[i] mod 10; a[i+1]:=a[i+1]+x; end;

    if a[i+1]<>0 then
    line:=line+1;

    for i:=line downto 1 do
    write(a[i]);

    //close(input); close(output);
    end;
    readln;
    end.

  • -1
    @ 2014-10-30 20:51:43

    其实是道python题
    print 2**input()*2-2

  • -1
    @ 2014-10-29 18:00:51

    #include <iostream>

    #include <string>
    using namespace std;
    int main ()
    {
    int n,l=1,a[1001]={0};
    cin>>n;
    n++;
    a[1]=1;
    for(int i=1;i<=n;i++)
    {
    for(int j=1;j<=100;j++)
    a[j]*=2;
    for(int j=1;j<=100;j++)
    {
    if(a[j]>=10)
    {
    a[j+1]+=a[j]/10;
    a[j]%=10;
    }
    }
    if(i==n-1)a[1]--;
    }
    for(int i=1;i<=1000;i++)
    if(a[i]!=0)l=max(i,l);
    for(int i=l;i>=1;i--)
    cout<<a[i];
    return 0;
    }

  • -1
    @ 2014-10-25 19:02:41

    #include<cstdio>
    #include<cstdlib>
    int ans[1000010]={1,1},n;
    int main()
    {
    scanf("%d",&n);
    for(int t=1;t<=n+1;t++)
    {
    ans[1]*=2;
    for(int i=1;i<=ans[0];i++)
    ans[i+1]*=2,ans[i+1]+=ans[i]/10,ans[i]%=10;
    ans[ans[0]+1]?ans[0]++:1;
    }
    ans[1]-=2;
    for(int i=2;i<=ans[0];i++)
    {
    if(ans[i-1]<0) ans[i-1]+=10,ans[i]--;
    else break;
    }
    for(int i=ans[0];i;i--)
    putchar(ans[i]+48);
    puts("");
    system("pause");
    return 0;
    }

  • -1
    @ 2014-10-25 19:02:19

    #include<cstdio>
    #include<cstdlib>
    int ans[1000010]={1,1},n;
    int main()
    {
    scanf("%d",&n);
    for(int t=1;t<=n+1;t++)
    {
    ans[1]*=2;
    for(int i=1;i<=ans[0];i++)
    ans[i+1]*=2,ans[i+1]+=ans[i]/10,ans[i]%=10;
    ans[ans[0]+1]?ans[0]++:1;
    }
    ans[1]-=2;
    for(int i=2;i<=ans[0];i++)
    {
    if(ans[i-1]<0) ans[i-1]+=10,ans[i]--;
    else break;
    }
    for(int i=ans[0];i;i--)
    putchar(ans[i]+48);
    puts("");
    system("pause");
    return 0;
    }

  • -1
    @ 2014-08-15 16:23:54

    function f(x:longint):longint;
    begin
    if x=1 then f:=1 else
    f:=f(x-1)*2+1
    end;
    var n:longint;
    begin
    read(n);
    write(2*f(n));
    end.

  • -1
    @ 2014-08-01 14:20:47

    题目分析:
    本题用记忆化递归,然后再加上高精度就可以过了。
    设F[n]表示有2*n个圆盘时完成题目所说的任务所需的最少移动次数,则
    F[n]:=2*f[n-1]+2;
    type data=array[0..1000] of longint;
    var
    n,i:longint;
    two,zero:data;
    f:array[0..200] of data;
    function add(a,b:data):data;
    var i,len:longint;
    c:data;
    begin
    fillchar(c,sizeof(c),0);
    if a[0]>b[0] then len:=a[0] else len:=b[0];
    for i:=1 to len do
    begin
    c[i]:=a[i]+b[i]+c[i];
    c[i+1]:=c[i] div 10+c[i+1];
    c[i]:=c[i] mod 10;
    end;
    if c[len+1]>0 then inc(len);
    c[0]:=len;
    exit(c);
    end;
    procedure sub(n:longint);
    begin
    if not((f[n,0]=1)and(f[n,1]=0)) then exit;
    sub(n-1);
    f[n]:=add(add(f[n-1],f[n-1]),two);
    end;
    begin
    readln(n);
    zero[0]:=1; zero[1]:=0;
    two[0]:=1; two[1]:=2;
    for i:=0 to n do f[i]:=zero;
    f[1]:=two;
    sub(n);
    for i:=f[n,0] downto 1 do write(f[n,i]);
    end.

信息

ID
1354
难度
5
分类
动态规划 点击显示
标签
递交数
4925
已通过
1816
通过率
37%
被复制
18
上传者