Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这个算法为什么WA?

Posted by sqybi at 2007-01-06 18:26:13 on Problem 2453
先转换二进制
找到第一个满足其后至少有一个1的0,然后将这个0改成1,接着把剩下的数按照0在前1在后的顺序覆盖在原来的数上
以样例为例
78转换成1001110
1001110找到左数第三位的0(最后一位的0右面没有1),这个0及它右面的数字中共有2个0和3个1
将这个0改成1(变成1011110)
这时剩下2个0和2个1(用了一个1)
再按照0011的顺序覆盖最后四位(变成1010011)
如果是1000这样的数就当作01000处理,结果是10000

附程序:
{
 Problem: An Easy Problem
 Writer: sqybi
 Date: 2007-01-06
 Result: Unknown
}
Program pku2453;
Var
  n, l, i, j, k1, k0, k: Longint;
  a: Array[1..200]Of Longint;

Begin
  ReadLn(n);
  While n <> 0 Do Begin
    l := 0;
    FillChar(a, SizeOf(a), 0);
    While n <> 0 Do Begin
      Inc(l);
      a[l] := n Mod 2;
      n := n Shr 1;
    End;
    Inc(l);
    k1 := 0;
    k0 := 0;
    For i:=1 To l Do Begin
      If a[i] = 1 Then
        Inc(k1)
      Else
        Inc(k0);
      If (a[i] = 0) And (k1 <> 0) Then Begin
        a[i] := 1;
        Dec(k1);
        For j:=1 To k1 Do a[j] := 1;
        For j:=k1+1 To k1+k0 Do a[j] := 0;
        Break;
      End;
    End;
    k := 1;
    For i:=1 To l Do Begin
      n := n + a[i] * k;
      k := k * 2;
    End;
    WriteLn(n);
    ReadLn(n);
  End;
End.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator