Section 2.1 Hamming Codes

来源:互联网 发布:软件视频会议 编辑:程序博客网 时间:2024/05/17 00:06

ans数组用来存结果

ans[1] 为0,然后枚举 知道找出n个

不必排序, 先找出来的一定小

至于判断两个数的海明距离, 用 A = A xor B 再A中1的个数即可

具体参加 百度百科 位运算

http://baike.baidu.com/view/379209.htm?fr=ala0_1

{
ID: yaoyuan4
PROG: hamming
LANG: PASCAL
}
Program hamming;
const
  inf = 'hamming.in'; outf = 'hamming.out';
var
  n, b, d : longint;
  ans : array[0..300] of longint;
Procedure init;
  begin
   assign(input, inf); reset(input);
   readln(n, b, d);
   close(input);
   ans[0] := 1; ans[1] := 0;
  end;
Function ok(a, b : longint) : boolean;
  var
   x, y : longint;
  begin
   x := a xor b;
   y := 0;
   repeat
    if odd(x) then inc(y);
    x := x shr 1;
   until x = 0;
   if y >= d then exit(true);
   exit(false);
  end;
Procedure work;
  var
   i, j, t : longint;
  begin
   i := 0;
   repeat
    inc(i);
    t := 0;
    for j := 1 to ans[0] do
     if ok(i, ans[j]) then inc(t);
    if t = ans[0] then
     begin
      inc(ans[0]);
      ans[ans[0]] := i;
     end;
   until ans[0] = n;
  end;
Procedure print;
  var
   i : longint;
  begin
   assign(output, outf); rewrite(output);
   for i := 1 to n do
    begin
     if i mod 10 = 1 then write(ans[i]) else write(' ',ans[i]);
     if i mod 10 = 0 then writeln;
    end;
   if n mod 10 <> 0 then writeln;
   close(output);
  end;
begin
  init;
  work;
  print;
end.

原创粉丝点击