题解

115 条题解

  • 0
    @ 2009-08-07 12:06:07

    我应该是AC这题程序最长的吧:

    program vijos1039;

    var

    t,n,i,j,tt:integer;

    k:array[0..10]of boolean;

    min,h:longint;

    y,a,s1,s2:array[1..10]of integer;

    function gz_jian(l1,l2:integer):longint;

    var

    ss1,ss2:string;

    i,a,b,code:longint;

    begin

    ss1:='';ss2:='';

    for i:=1 to l1 do ss1:=ss1+char(48+s1[i]);

    for i:=1 to l2 do ss2:=ss2+char(48+s2[i]);

    val(ss1,a,code);val(ss2,b,code);

    exit(a-b);

    end;

    begin

    readln(t);

    repeat

    dec(t);

    readln(n);

    for i:=1 to n do read(a[i]);

    for i:=1 to n-1 do

    for j:=i+1 to n do

    if a[i]>a[j] then begin

    tt:=a[i];

    a[i]:=a[j];

    a[j]:=tt;

    end;

    i:=1;

    j:=1;

    y[1]:=a[1];

    repeat

    inc(i);inc(j);

    if (a[i]=a)and(n mod 2=0) then j:=j-2

    else y[j]:=a[i];

    until i=n;

    n:=j;

    fillchar(k,sizeof(k),true);

    if n mod 2 =1 then begin

    fillchar(k,sizeof(k),true);

    i:=1;

    if y[i]=0 then inc(i);

    s1[1]:=y[i];

    j:=1;

    k[i]:=false;

    for i:=1 to n do

    if k[i] then begin

    inc(j);

    s1[j]:=y[i];

    k[i]:=false;

    if j=n div 2+1 then break;

    end;

    j:=0;

    for i:=n downto 1 do

    if k[i] then begin

    inc(j);

    s2[j]:=y[i];

    k[i]:=false;

    if j=n div 2 then break;

    end;

    writeln(gz_jian(n div 2+1,n div 2));

    end else begin

    min:=4000000;

    if n=2 then min:=abs(y[1]-y[2]);

    if n=0 then min:=0;

    for i:=1 to n-1 do if y[i]0 then begin

    fillchar(k,sizeof(k),true);

    k[i]:=false;k:=false;

    j:=1;

    s2[j]:=y[i];s1[j]:=y;

    for h:=1 to n do

    if k[h] then begin

    inc(j);

    s1[j]:=y[h];

    k[h]:=false;

    if j= n div 2 then break;

    end;

    j:=1;

    for h:=n downto 1 do

    if k[h] then begin

    inc(j);

    s2[j]:=y[h];

    k[h]:=false;

    if j= n div 2 then break;

    end;

    if gz_jian(n div 2,n div 2)

  • 0
    @ 2009-08-03 18:16:56

    计数排序.....

  • 0
    @ 2009-08-02 21:49:45

    victorygod的数据太错了!!!大家别看!

  • 0
    @ 2009-08-02 17:33:51

    好像victorygod的数据中

    10

    0 1 2 3 4 5 6 7 8 9

    的答案应该是247

  • 0
    @ 2009-08-02 15:00:51

    为什么会非法存取?

  • 0
    @ 2009-07-24 23:15:44

    有10、、、被雷了、、

  • 0
    @ 2009-07-22 19:30:17

    做这种题。还不如跳楼好.崩溃了.巨猥琐.

  • 0
    @ 2009-07-11 10:27:52

    楼下的,412-365不的更小吗?

    鄙视楼下给数据的,连10都搞出来了!

  • 0
    @ 2009-07-07 10:27:05

    QQ里有人让我帮看一下这题...觉得不难..来做了一下..结果陷进去了..囧..

    ---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

    感谢...jsydtc的题解....

    看来奇数情况确实比较简单...这个...偶数的情况则不一样..

    看来我错在只从相邻的数里找了最靠前的那组最小的...

    {

    枚举开头的两个数,而且两数相邻。(其实枚举差最小的几组就行,

    不过 10 的规模,不用那么麻烦了)再给大的从剩下找一组最小的,

    小的找一最大的。如 1 2 3 4 5 6,枚举1 2 时,234-165。

    }

    另一方面...完全没有考虑...重复的情况...

    看了一下题目下面的提示...想了一下存数据有两个方案...

    a:array[1..nn] of int; 存具体的数字,这个判重会麻烦些...

    c:array[0..10] of boolean; 这样的话判断数据重复会方便许多...

    但是其它操作则不如方案1...

    我觉得数字总数比较小么...选前者会方便些...哎

  • 0
    @ 2009-06-06 20:52:21

    var i,j,ii,k1,k2,m,n,t,s,ss,k,tot,min,x:longint;

    c:array[1..10] of byte;

    a:array[1..10] of byte;

    begin

    readln(n);

    for k:=1 to n do begin

    readln(m);

    for i:=1 to m do read(a[i]);

    for i:=m-1 downto 1 do

    for j:=1 to i do

    if a[j]>a[j+1] then begin

    x:=a[j];

    a[j]:=a[j+1];

    a[j+1]:=x;

    end;

    if m=2 then begin

    writeln(a[2]-a[1]);

    continue;

    end;

    if odd(m) then begin

    if a[1]=0 then t:=2 else t:=1;

    s:=a[t];

    dec(m);

    for i:=t to m do

    a[i]:=a;

    ss:=0;

    for i:=1 to m div 2 do begin

    s:=s*10+a[i];

    ss:=ss*10+a[m+1-i];

    end;

    writeln(s-ss);

    end else begin

    if a[1]=0 then t:=2 else t:=1;

    min:=100;

    for i:=t to m-1 do

    if a-a[i]

  • 0
    @ 2009-05-25 21:15:22

    var i,j,ii,k1,k2,m,n,t,s,ss,k,tot,min,x:longint;

    c:array[1..10] of byte;

    a:array[1..10] of byte;

    begin

    readln(n);

    for k:=1 to n do begin

    readln(m);

    for i:=1 to m do read(a[i]);

    for i:=m-1 downto 1 do

    for j:=1 to i do

    if a[j]>a[j+1] then begin

    x:=a[j];

    a[j]:=a[j+1];

    a[j+1]:=x;

    end;

    if m=2 then begin

    writeln(a[2]-a[1]);

    continue;

    end;

    if odd(m) then begin

    if a[1]=0 then t:=2 else t:=1;

    s:=a[t];

    dec(m);

    for i:=t to m do

    a[i]:=a;

    ss:=0;

    for i:=1 to m div 2 do begin

    s:=s*10+a[i];

    ss:=ss*10+a[m+1-i];

    end;

    writeln(s-ss);

    end else begin

    if a[1]=0 then t:=2 else t:=1;

    min:=100;

    for i:=t to m-1 do

    if a-a[i]

  • 0
    @ 2009-05-15 14:17:27

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 0ms

    ├ 测试数据 07:答案正确... 0ms

    ├ 测试数据 08:答案正确... 0ms

    ├ 测试数据 09:答案正确... 0ms

    ├ 测试数据 10:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

    水啊水!!!

    var i,j,ii,k1,k2,m,n,t,s,ss,k,tot,min,x:longint;

    c:array[1..10] of byte;

    a:array[1..10] of byte;

    begin

    readln(n);

    for k:=1 to n do begin

    readln(m);

    for i:=1 to m do read(a[i]);

    for i:=m-1 downto 1 do

    for j:=1 to i do

    if a[j]>a[j+1] then begin

    x:=a[j];

    a[j]:=a[j+1];

    a[j+1]:=x;

    end;

    if m=2 then begin

    writeln(a[2]-a[1]);

    continue;

    end;

    if odd(m) then begin

    if a[1]=0 then t:=2 else t:=1;

    s:=a[t];

    dec(m);

    for i:=t to m do

    a[i]:=a;

    ss:=0;

    for i:=1 to m div 2 do begin

    s:=s*10+a[i];

    ss:=ss*10+a[m+1-i];

    end;

    writeln(s-ss);

    end else begin

    if a[1]=0 then t:=2 else t:=1;

    min:=100;

    for i:=t to m-1 do

    if a-a[i]

  • 0
    @ 2009-02-17 22:28:53

    天诛地灭啊...

    测试数据貌似有许多种情况都没有考虑到,楼下victorygod给出的数据错的一塌糊涂,他竟然也能过!

    不必考虑太多了...像“只有一个正数和N个0”的情况是不会出现的,特别考虑0不能做首位即可...

    放心大胆地提交吧,ac了这看起来难上加难的恶心题.....

  • 0
    @ 2009-01-30 02:22:55

    能AC此种该死之题!!!!………………

    整个高中都没做出来这道题!!!………………

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 0ms

    ├ 测试数据 07:答案正确... 0ms

    ├ 测试数据 08:答案正确... 0ms

    ├ 测试数据 09:答案正确... 0ms

    ├ 测试数据 10:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

    var

    a,b,bb:array[0..50] of integer;

    i,j,k,n,pp:integer;

    num,a1,a2,t:longint;

    p:boolean;

    begin

    readln(num);

    while num>0 do

    begin

    dec(num);

    fillchar(a,sizeof(a),0);

    a1:=0;a2:=0;

    readln(n);

    for i:=1 to n do read(a[i]);

    for i:=1 to n-1 do

    for j:=n-1 downto i do

    if a[j]>a[j+1] then begin t:=a[j+1];a[j+1]:=a[j];a[j]:=t; end;

    //for i:=1 to n do write(a[i],' '); writeln;

    if n=1 then begin writeln(a[1]);continue; end;

    if n=2 then begin writeln(abs(a[1]-a[2]));continue; end;

    if n and 1=0 then

    begin

    p:=true;pp:=0;

    j:=0;i:=1;

    while ia2-a1 then t:=a2-a1;//writeln(a1,' ',a2);

    //for i:=1 to n-2 do write(b[i],' ');writeln;

    end;

    writeln(t);

    end else

    begin

    if a[n-1]=0 then

    begin

    a1:=a[n];

    for i:=1 to n-2 do a1:=a1*10;

    writeln(a1);continue;

    end;

    if a[1]=0 then

    begin

    i:=1;

    while a[i]=0 do inc(i);

    a[1]:=a[i];a[i]:=0;

    end;

    a1:=a[1];a2:=0;

    for i:=2 to (n+1) div 2 do

    a1:=a1*10+a[i];

    for i:=n downto (n+1) div 2+1 do

    a2:=a2*10+a[i];

    writeln(a1-a2);

    end;

    end;

    end.

    算法大家已经很清楚了吧。

    给几组数据:

    22

    6

    0 1 2 4 6 7

    4

    1 6 3 4

    1

    3

    3

    0 4 2

    1

    10

    0 1 2 3 4 5 6 7 8 9

    2

    1 0

    5

    3 6 1 9 0

    6

    2 4 3 6 2 0

    7

    1 0 5 4 4 2 9

    6

    3 0 0 0 0 0

    7

    9 0 0 0 0 0 0

    6

    1 3 0 0 0 0

    5

    4 2 0 0 0

    8

    1 3 5 5 0 0 0 0

    8

    2 2 5 9 3 4 0 0

    5

    0 1 2 3 4

    3

    5 9 3

    5

    3 9 7 8 5

    5

    6 7 8 9 10

    7

    3 4 6 7 8 9 10

    10

    0 1 7 5 4 4 5 7 8 9

    ans:

    28

    5

    3

    16

    147

    1

    7

    4

    70

    300

    900000

    200

    160

    2

    4

    59

    26

    259

    569

    2369

    9

  • 0
    @ 2009-01-23 21:35:10

    TNND 鸟题,诿题,从初中做到高中

    TNND 有够委琐的偶数位时0不能做首位奇数位时却可以

    TNND 做的头皮抓破- -

  • 0
    @ 2009-01-10 15:38:51

    在本地枚举减数和被减数(0到100000),打一个 int r[1024] 的表好了..

    r[i]中的i表示1~9出现了哪些, i&(1

  • 0
    @ 2008-12-20 10:42:20

    首先要判断是否只有2个数,

    否则的话,除非你的程序够强悍才能AC.

  • 0
    @ 2008-12-12 20:37:30

     ┌──┬──╂──┬──╂──┐

    4│  │ × ┃ × │  ┃  │

     ├──┼──╂──┼──╂──┤

    3│  │  ┃ ※ │  ┃  │

     ┿━━┿━━╋━━┿━━╋━━┿

    2│  │  ┃ ※ │ + ┃ + │

     ├──┼──╂──┼──╂──┤

    1│  │  ┃  │  ┃  │

     └──┴──╂──┴──╂──┘

       1  2  3  4  5

  • 0
    @ 2008-11-30 03:49:42

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 0ms

    ├ 测试数据 07:答案正确... 0ms

    ├ 测试数据 08:答案正确... 0ms

    ├ 测试数据 09:答案正确... 0ms

    ├ 测试数据 10:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

    提交了4次

    第4次本打算玩着交

    没想到AC了

  • 0
    @ 2008-11-04 21:43:23

    SAVE 下程序

    type

    tlist=array[1..10]of longint;

    var

    i,j,t,n,s,k,h,min,sum,sum1,q:longint;

    a,b:array[1..10]of longint;

    procedure qsort(var a:tlist;s,t:longint);

    var

    i,j,x,y:longint;

    begin

    i:=s;

    j:=t;

    x:=a[(i+j) shr 1];

    repeat

    while a[i]x do

    dec(j);

    if ij;

    if is

    then qsort(a,s,j);

    end;

    procedure jzero;

    var

    i,j,k:longint;

    begin

    i:=1;

    while a[i]=0 do

    inc(i);

    a[1]:=a[i];

    for j:=2 to i do

    a[j]:=0;

    end;

    begin

    readln(t);

    for h:=1 to t do

    begin

    readln(n);

    min:=51314520;

    fillchar(a,sizeof(a),$ff);

    for i:=1 to n do

    read(a[i]);

    qsort(a,1,n);

    if n and 1=1

    then begin

    jzero;

    sum:=0;

    for i:=1 to n shr 1+1 do

    sum:=sum*10+a[i];

    sum1:=0;

    for i:=n downto n shr 1+2 do

    sum1:=sum1*10+a[i];

    writeln(sum-sum1)

    end

    else begin

    i:=1;

    repeat

    j:=i+1;

    k:=1;

    while a[j]=a[i] do

    begin

    inc(K);

    inc(j);

    end;

    if odd(k)

    then begin

    for s:=i+1 to n-k+1 do

    a:=a;

    end

    else begin

    for s:=i to n-k do

    a:=a;

    dec(i);

    end;

    if odd(k)

    then dec(n,k-1)

    else dec(n,k);

    inc(i);

    until i>=n;

    if n and 1=1

    then begin

    jzero;

    sum:=0;

    for i:=1 to n shr 1+1 do

    sum:=sum*10+a[i];

    sum1:=0;

    for i:=n downto n shr 1+2 do

    sum1:=sum1*10+a[i];

    writeln(sum-sum1)

    end

    else begin

    for k:=1 to n-1 do

    if a[k]0

    then begin

    sum:=a[k+1]-a[k];

    j:=0;

    for i:=1 to n do

    if (ik)and(ik+1)

    then begin

    inc(j);

    sum:=sum*10+a[i];

    if j=n shr 1-1

    then break;

    end;

    sum1:=0;

    for j:=n downto i+1 do

    if (jk)and(jk+1)

    then sum1:=sum1*10+a[j];

    if sum-sum1

信息

ID
1039
难度
7
分类
贪心 点击显示
标签
(无)
递交数
3685
已通过
825
通过率
22%
被复制
11
上传者