题解

111 条题解

  • 0
    @ 2015-08-01 16:18:28

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;

    void out(int a){
    int bri, h=0;
    while(a > 0){
    bri = 1;
    h=0;
    while(a >= bri*2){
    bri *= 2;
    h++;
    }
    if(bri > 2){
    cout<<"2(";
    out(h);
    cout<<")";
    }
    else if(bri == 2)
    cout<<"2";
    else
    cout<<"2(0)";
    a -= bri;
    if(a > 0)
    cout<<"+";
    }
    }

    int main()
    {
    int n;
    cin>>n;

    out(n);
    system("pause");
    return 0;
    }
    有点意思-_-

  • 0
    @ 2015-06-12 18:53:41

    打表
    program p1914;
    const a:array[0..14] of integer=(1,
    2,
    4,
    8,
    16,
    32,
    64,
    128,
    256,
    512,
    1024,
    2048,
    4096,
    8192,
    16384);
    b:array[0..14] of string=('2(0)',
    '2',
    '2(2)',
    '2(2+2(0))',
    '2(2(2))',
    '2(2(2)+2(0))',
    '2(2(2)+2)',
    '2(2(2)+2+2(0))',
    '2(2(2+2(0)))',
    '2(2(2+2(0))+2(0))',
    '2(2(2+2(0))+2)',
    '2(2(2+2(0))+2+2(0))',
    '2(2(2+2(0))+2(2))',
    '2(2(2+2(0))+2(2)+2(0))',
    '2(2(2+2(0))+2(2)+2)');
    var i,j,n:integer;
    begin
    readln(n);
    for i:=14 downto 0 do
    if n>=a[i] then
    begin
    n:=n-a[i];
    write(b[i]);
    break;
    end;
    dec(i);
    for j:=i downto 0 do
    if n>=a[j] then
    begin
    n:=n-a[j];
    write('+',b[j]);
    end;
    writeln;
    end.

  • 0
    @ 2015-05-05 15:31:41

    打表最光荣

  • 0
    @ 2015-01-24 21:01:57

    二进制+递推
    本来打算用递归的,但是想想递推就够了。
    细节处理上有点麻烦。

    Pascal Code

    var
    n,i,k,j,h,l:longint;
    a:array[0..20000] of string;
    t:array[1..100] of longint;
    flag:boolean;

    begin
    readln(n);
    for i:=0 to 20000 do a[i]:='';
    a[0]:='0';
    a[1]:='2(0)';
    a[2]:='2';
    for i:=3 to n do
    begin
    k:=i;
    j:=0;
    fillchar(t,sizeof(t),0);
    l:=0;
    flag:=false;
    repeat //二进制转换
    inc(j);
    t[j]:=k mod 2;
    if (t[j]=0) and not flag then inc(l) //寻找最低非0位
    else flag:=true; //找到非0位
    k:=k div 2;
    until k=0;
    for h:=j downto l+1 do
    begin
    if t[h]=0 then continue; //t[h]=0时不输出
    if (h=l+1) and (h<>2) then begin a[i]:=a[i]+'2('+a[h-1]+')';continue;end; //最后一次不输出+号
    if (h=l+1) and (h=2) then begin a[i]:=a[i]+'2';continue;end; //最后一次不输出+号,避免输出2(2(0))
    if h=2 then begin a[i]:=a[i]+'2+';continue;end; //避免输出2(2(0))
    a[i]:=a[i]+'2('+a[h-1]+')+'; //递推
    end;
    end;
    writeln(a[n]);
    end.

  • 0
    @ 2014-10-27 18:35:58

    NOIP2014赛前AC留念
    (最讨厌表达式什么的了~~~~~~);
    var n,i,tip:longint;
    num,t:array[0..25] of longint;
    function tip1(k:longint):boolean;
    var i:longint;
    begin
    for i:=k-1 downto 0 do
    if t[i]=1 then exit(true);
    exit(false);
    end;

    function tip2(k:longint):boolean;
    var i:longint;
    begin
    for i:=k-1 downto 0 do
    if num[i]=1 then exit(true);
    exit(false);
    end;

    procedure devide(k:longint);
    var i,t1:longint;
    old:array[0..25] of longint;
    begin
    fillchar(t,sizeof(t),0);
    t1:=0;
    while k<>0 do
    begin
    t[t1]:=k mod 2;
    k:=k div 2;
    inc(t1);
    end;
    old:=t;
    for i:=t1-1 downto 0 do
    if t[i]<>0 then begin
    if i>2 then begin
    write('2(');
    devide(i);
    t:=old;
    write(')');
    end;
    if i=2 then write('2(2)');
    if i=1 then write('2');
    if i=0 then write('2(0)');
    if tip1(i) then write('+');
    end;
    end;

    begin
    //assign(input,'t3.in');
    //assign(output,'t3.out');
    //reset(input);
    //rewrite(output);
    readln(n);
    while n<>0 do
    begin
    num[tip]:=n mod 2;
    n:=n div 2;
    inc(tip);
    end;
    for i:=tip-1 downto 0 do
    begin
    if num[i]<>0 then begin
    if i>2 then begin
    write('2(');
    devide(i);
    write(')');
    end;
    if i=2 then write('2(2)');
    if i=1 then write('2');
    if i=0 then write('2(0)');
    if tip2(i) then write('+')
    else writeln;
    end;
    end;
    //close(input);
    //close(output);
    end.

  • 0
    @ 2014-08-12 10:07:42

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int a[16]={1},n,x=0;
    string s[1001];
    int work(int x)
    {
    int b,t=x;
    if (x==0) return 0;
    for (int i=0;i<=15;i++)
    if (x<a[i])
    {
    x-=a[i-1];
    b=i-1;
    break;
    }
    switch (b)
    {
    case 0:{cout<<"2(0)";break;}
    case 1:{cout<<"2";break;}
    case 2:{cout<<"2(2)";break;}
    case 3:{cout<<"2(2+2(0))";break;}
    case 4:{cout<<"2(2(2))";break;}
    case 5:{cout<<"2(2(2)+2(0))";break;}
    case 6:{cout<<"2(2(2)+2)";break;}
    case 7:{cout<<"2(2(2)+2+2(0))";break;}
    case 8:{cout<<"2(2(2+2(0)))";break;}
    case 9:{cout<<"2(2(2+2(0))+2(0))";break;}
    case 10:{cout<<"2(2(2+2(0))+2)";break;}
    case 11:{cout<<"2(2(2+2(0))+2+2(0))";break;}
    case 12:{cout<<"2(2(2+2(0))+2(2))";break;}
    case 13:{cout<<"2(2(2+2(0))+2(2)+2(0))";break;}
    case 14:{cout<<"2(2(2+2(0))+2(2)+2)";break;}
    }
    if (x>0) cout<<'+';
    work(x);
    }
    main()
    {
    //freopen("power.in","r",stdin);
    //freopen("power.out","w",stdout);
    for (int i=1;i<=15;i++)
    a[i]=a[i-1]*2;
    cin>>n;
    work(n);
    //fclose(stdin);fclose(stdout);
    }

  • 0
    @ 2014-07-15 19:42:58

    program _1597cifang2;
    const two:array[0..15]of longint=(1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768);
    var n:integer;
    procedure writewrite(var z:integer);
    begin
    write('2');
    case z of
    0:write('(0');
    1:exit;
    2:write('(2');
    3:write('(2+2(0)');
    4:write('(2(2)');
    5:write('(2(2)+2(0)');
    6:write('(2(2)+2');
    7:write('(2(2)+2+2(0)');
    8:write('(2(2+2(0))');
    9:write('(2(2+2(0))+2(0)');
    10:write('(2(2+2(0))+2');
    11:write('(2(2+2(0))+2+2(0)');
    12:write('(2(2+2(0))+2(2)');
    13:write('(2(2+2(0))+2(2)+2(0)');
    14:write('(2(2+2(0))+2(2)+2');
    end;
    write(')');
    end;

    procedure cifang;
    var i:integer;
    begin
    while n<>0 do
    begin
    for i:=0 to 15 do
    if (two[i]<=n)and(n<two[i+1]) then
    break;
    writewrite(i);
    n:=n-two[i];
    if n<>0 then write('+');
    end;
    end;

    begin
    read(n);
    cifang;
    end.
    打表秒杀

  • 0
    @ 2014-04-02 18:01:43

    const
    x:array[0..14] of string=('2(0)','2','2(2)','2(2+2(0))','2(2(2))',
    '2(2(2)+2(0))','2(2(2)+2)','2(2(2)+2+2(0))',
    '2(2(2+2(0)))','2(2(2+2(0))+2(0))','2(2(2+2(0))+2)',
    '2(2(2+2(0))+2+2(0))','2(2(2+2(0))+2(2))',
    '2(2(2+2(0))+2(2)+2(0))','2(2(2+2(0))+2(2)+2)');
    var
    a,b,c:longint;
    begin
    read(a);
    while a>0 do
    begin
    write(x[trunc(ln(a)/ln(2)+0.00001)]);
    a:=a-1 shl trunc(ln(a)/ln(2)+0.00001);
    if a>0 then write('+');
    end;
    end.

  • 0
    @ 2014-03-07 20:38:06

    简单的递归。。。。

  • 0
    @ 2013-11-05 23:34:58

    评测结果

    编译成功

    测试数据 #0: Accepted, time = 0 ms, mem = 468 KiB, score = 10

    测试数据 #1: Accepted, time = 15 ms, mem = 468 KiB, score = 10

    测试数据 #2: Accepted, time = 0 ms, mem = 460 KiB, score = 10

    测试数据 #3: Accepted, time = 0 ms, mem = 468 KiB, score = 10

    测试数据 #4: Accepted, time = 0 ms, mem = 464 KiB, score = 10

    测试数据 #5: Accepted, time = 0 ms, mem = 468 KiB, score = 10

    测试数据 #6: Accepted, time = 0 ms, mem = 468 KiB, score = 10

    测试数据 #7: Accepted, time = 15 ms, mem = 468 KiB, score = 10

    测试数据 #8: Accepted, time = 0 ms, mem = 468 KiB, score = 10

    测试数据 #9: Accepted, time = 15 ms, mem = 464 KiB, score = 10

    Accepted, time = 45 ms, mem = 468 KiB, score = 100
    代码

    #include <stdio.h>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <math.h>
    #include <cstdlib>

    using namespace std;

    int n;
    int a[14 + 1];
    int b[14 + 1];
    int i;
    int first;

    int main()
    {
    while( scanf( "%d" , &n ) != EOF )
    {
    first = 1;
    memset( a , 0 , sizeof( a ) );
    memset( b , 0 , sizeof( b ) );
    a[0] = 1;
    for( i = 1 ; i < 15 ; i++ )
    a[i] = a[i - 1] * 2;
    for( i = 14 ; i >= 0 ; i-- )
    if( n >= a[i] )
    {
    b[i] = 1;
    n -= a[i];
    }
    for( i = 14 ; i >= 0 ; i-- )
    {
    if( b[i] == 1 )
    {
    if( first )
    first = 0;
    else
    cout << "+";
    if( i == 14 )
    cout << "2(2(2+2(0))+2(2)+2)";
    else if( i == 13 )
    cout << "2(2(2+2(0))+2(2)+2(0))";
    else if( i == 12 )
    cout << "2(2(2+2(0))+2(2))";
    else if( i == 11 )
    cout << "2(2(2+2(0))+2+2(0))";
    else if( i == 10 )
    cout << "2(2(2+2(0))+2)";
    else if( i == 9 )
    cout << "2(2(2+2(0))+2(0))";
    else if( i == 8 )
    cout << "2(2(2+2(0)))";
    else if( i == 7 )
    cout << "2(2(2)+2+2(0))";
    else if( i == 6 )
    cout << "2(2(2)+2)";
    else if( i == 5 )
    cout << "2(2(2)+2(0))";
    else if( i == 4 )
    cout << "2(2(2))";
    else if( i == 3 )
    cout << "2(2+2(0))";
    else if( i == 2 )
    cout << "2(2)";
    else if( i == 1 )
    cout << "2";
    else
    cout << "2(0)";
    }
    }
    cout << endl;
    }
    return 0;
    }

  • 0
    @ 2013-11-03 10:26:26

    #include<iostream>
    #include<stdio.h>
    int n;
    void print(int i)
    {
    switch(i)
    {
    case 0:{printf("2(0)");break;}
    case 1:{printf("2");break;}
    case 2:{printf("2(2)");break;}
    case 3:{printf("2(2+2(0))");break;}
    case 4:{printf("2(2(2))");break;}
    case 5:{printf("2(2(2)+2(0))");break;}
    case 6:{printf("2(2(2)+2)");break;}
    case 7:{printf("2(2(2)+2+2(0))");break;}
    case 8:{printf("2(2(2+2(0)))");break;}
    case 9:{printf("2(2(2+2(0))+2(0))");break;}
    case 10:{printf("2(2(2+2(0))+2)");break;}
    case 11:{printf("2(2(2+2(0))+2+2(0))");break;}
    case 12:{printf("2(2(2+2(0))+2(2))");break;}
    case 13:{printf("2(2(2+2(0))+2(2)+2(0))");break;}
    case 14:{printf("2(2(2+2(0))+2(2)+2)");}
    }
    }
    void bijiao()
    {
    int i,t;
    while(n!=0)
    {
    t=1;
    i=0;
    while(n>=t*2) {
    t=t*2; ++i;
    }
    n=n-t;
    print(i);
    if(n!=0) printf("+");
    }

    }
    int main()
    {
    scanf("%d",&n);
    bijiao();
    //system("pause");
    return 0;
    }

  • 0
    @ 2013-10-22 20:08:20

    每当我一次AC的时候就很兴奋。
    递归即可。。
    SYF

  • 0
    @ 2013-09-30 22:22:33

    So easy~~~把数字转换成2进制看看!!!
    var
    i,j,k,l,n,m,o,p:longint;
    a:array[0..31]of longint;
    function work(x:longint):ansistring;
    var
    i,j:longint;
    s:ansistring;
    begin
    if x=0 then exit('0');
    if x=1 then exit('');
    if x=2 then exit('2');
    i:=0;
    s:='';
    while x>0 do
    begin
    if odd(x mod 2) then if i<>1 then work:='+2('+work(i)+')'+s
    else work:='+2'+s;
    s:=work;
    x:=x div 2;
    inc(i);
    end;
    delete(work,1,1);
    end;
    begin
    readln(n);
    if n=1 then writeln('2(0)')//被这个细节坑了一次5555
    else
    writeln(work(n));
    end.

  • 0
    @ 2012-11-05 17:03:31

    var

    n,i,t:longint;

    s:ansistring;

    procedure f(n:longint);

    begin

    case n of

    0:write('2(0)');

    1:write('2');

    2:write('2(2)');

    3:write('2(2+2(0))');

    4:write('2(2(2))');

    5:write('2(2(2)+2(0))');

    6:write('2(2(2)+2)');

    7:write('2(2(2)+2+2(0))');

    8:write('2(2(2+2(0)))');

    9:write('2(2(2+2(0))+2(0))');

    10:write('2(2(2+2(0))+2)');

    11:write('2(2(2+2(0))+2+2(0))');

    12:write('2(2(2+2(0))+2(2))');

    13:write('2(2(2+2(0))+2(2)+2(0))');

    14:write('2(2(2+2(0))+2(2)+2)');

    end;

    end;

    begin

    readln(n);

    while n>0 do

    begin

      t:=1;

     i:=0;

     while n>=t*2 do

    begin

       t:=t*2; inc(i);

      end;

      n:=n-t;

      f(i);

     if n>0 then write('+');

    end;

    end.

  • 0
    @ 2012-11-02 12:31:10

    var

    n:longint;

    s:ansistring;

    procedure f(n:integer);

    begin

      case n of

       0:write('2(0)');

       1:write('2');

       2:write('2(2)');

       3:write('2(2+2(0))');

       4:write('2(2(2))');

       5:write('2(2(2)+2(0))');

       6:write('2(2(2)+2)');

       7:write('2(2(2)+2+2(0))');

       8:write('2(2(2+2(0)))');

       9:write('2(2(2+2(0))+2(0))');

       10:write('2(2(2+2(0))+2)');

       11:write('2(2(2+2(0))+2+2(0))');

       12:write('2(2(2+2(0))+2(2))');

       13:write('2(2(2+2(0))+2(2)+2(0))');

       14:write('2(2(2+2(0))+2(2)+2)');

      end;

    end;

    procedure c;

    var

      i,t:integer;

    begin

      while n>0 do begin

       t:=1;

       i:=0;

       while n>=t*2 do begin

        t:=t*2;

        inc(i);

       end;

       n:=n-t;

       f(i);

       if n>0 then write('+');

      end;

    end;

    begin

    readln(n);

    end.

  • 0
    @ 2012-10-07 08:14:32

    列情况,秒过

    var

    n:longint;

    s:ansistring;

    procedure f(n:integer);

    begin

    case n of

    0:write('2(0)');

    1:write('2');

    2:write('2(2)');

    3:write('2(2+2(0))');

    4:write('2(2(2))');

    5:write('2(2(2)+2(0))');

    6:write('2(2(2)+2)');

    7:write('2(2(2)+2+2(0))');

    8:write('2(2(2+2(0)))');

    9:write('2(2(2+2(0))+2(0))');

    10:write('2(2(2+2(0))+2)');

    11:write('2(2(2+2(0))+2+2(0))');

    12:write('2(2(2+2(0))+2(2))');

    13:write('2(2(2+2(0))+2(2)+2(0))');

    14:write('2(2(2+2(0))+2(2)+2)');

    end;

    end;

    procedure c;

    var

    i,t:integer;

    begin

    while n>0 do begin

    t:=1;

    i:=0;

    while n>=t*2 do begin

    t:=t*2;

    inc(i);

    end;

    n:=n-t;

    f(i);

    if n>0 then write('+');

    end;

    end;

    begin

    readln(n);

    c;

    end.

  • 0
    @ 2012-09-10 12:52:30

    program p1

    var i,j:longint;

    begin

    if n=0 then exit;

    i:=2;

    j:=1;

    while in then

    begin

    i:=i div 2;

    j:=j-1;

    end;

    n:=n-i;

    if j

  • 0
    @ 2010-07-06 21:58:20

    var

    n,l:longint;

    m:ansistring;

    procedure w(n:integer);

    var i,j:longint;

    begin

    if n=0 then exit;

    i:=2;

    j:=1;

    while in then

    begin

    i:=i div 2;

    j:=j-1;

    end;

    n:=n-i;

    if j

  • 0
    @ 2009-11-15 20:00:07

    #include

    int n;

    void print(int i)

    {

    switch(i)

    {

    case 0:{printf("2(0)");break;}

    case 1:{printf("2");break;}

    case 2:{printf("2(2)");break;}

    case 3:{printf("2(2+2(0))");break;}

    case 4:{printf("2(2(2))");break;}

    case 5:{printf("2(2(2)+2(0))");break;}

    case 6:{printf("2(2(2)+2)");break;}

    case 7:{printf("2(2(2)+2+2(0))");break;}

    case 8:{printf("2(2(2+2(0)))");break;}

    case 9:{printf("2(2(2+2(0))+2(0))");break;}

    case 10:{printf("2(2(2+2(0))+2)");break;}

    case 11:{printf("2(2(2+2(0))+2+2(0))");break;}

    case 12:{printf("2(2(2+2(0))+2(2))");break;}

    case 13:{printf("2(2(2+2(0))+2(2)+2(0))");break;}

    case 14:{printf("2(2(2+2(0))+2(2)+2)");}

    }

    }

    void bijiao()

    {

    int i,t;

    while(n!=0)

    {

    t=1;

    i=0;

    while(n>=t*2) {t=t*2; ++i;}

    n=n-t;

    print(i);

    if(n!=0) printf("+");

    }

    }

    int main()

    {

    scanf("%d",&n);

    bijiao();

    return 0;

    }

  • 0
    @ 2009-11-09 22:20:30

    自来水太多了

    直接case14种可能的方案,每个中间加‘+’就好了

信息

ID
1597
难度
3
分类
模拟 点击显示
标签
递交数
2824
已通过
1420
通过率
50%
被复制
10
上传者