POJ 1832(9连环)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nike0good/article/details/7878680

格雷码做

0要特判


答案 就是读入的两个数充格雷码转为2进制的差的绝对值的十进制


Program P1832;
Type
   arr=record
       a:array[1..500] of longint;
       len:longint;
       end;
var
   n,m,i:longint;
   a,b,c,d:arr;
   node2:array[1..1000] of arr;
function max(a,b:longint):longint;
begin
   if a<b then exit(b) else exit(a);
end;
function compare:boolean;
var
   i,j:longint;
begin
   while (a.len>1) and (a.a[a.len]=0) do dec(a.len);
   while (b.len>1) and (b.a[b.len]=0) do dec(b.len);
   if a.len<b.len then exit(true)
   else if a.len>b.len then exit(false);

   i:=a.len;
   while (i>1) and (a.a[i]=b.a[i]) do
   begin
      dec(i);
   end;
   if (a.a[i]<=b.a[i]) then exit(true) else exit(false);
end;
Procedure Subtract(a,b:arr;var c:arr);
var
   i,j:longint;
   pa:arr;
begin
   if (compare) then
   begin
      pa:=a;
      a:=b;
      b:=pa;
   end;
   fillchar(c,sizeof(c),0);
   for i:=1 to a.len do
   begin
      inc(c.a[i],a.a[i]-b.a[i]);
      if c.a[i]<0 then
      begin
         inc(c.a[i],2);
         dec(c.a[i+1]);
      end;
   end;
   c.len:=a.len;
   while (c.len>1) and (c.a[c.len]=0) do dec(c.len);
end;
procedure add(a,b:arr;var c:arr);
var
   i,j:longint;
begin
   fillchar(c,sizeof(c),0);
   for i:=1 to max(a.len,b.len) do
   begin
      inc(c.a[i],a.a[i]+b.a[i]);
      inc(c.a[i+1],c.a[i] div 10);
      c.a[i]:=c.a[i] mod 10;
   end;
   c.len:=max(a.len,b.len)+1;
   while (c.len>1) and (c.a[c.len]=0) do dec(c.len);
end;
begin
   fillchar(node2,sizeof(node2),0);
   node2[1].len:=1;
   node2[1].a[1]:=1;

   for i:=2 to 500 do
   begin
      add(node2[i-1],node2[i-1],node2[i]);
   end;

 {  assign(input,'P1832.in');
   assign(output,'P1832.out');
   reset(input);
   rewrite(output);
  }
   read(m);
   while m>0 do
   begin
      fillchar(a,sizeof(a),0);
      fillchar(b,sizeof(b),0);
      read(n);

      if n=0 then
      begin
         writeln('0');
         dec(m);
         continue;
      end;
      for i:=n downto 1 do read(a.a[i]);
      for i:=n downto 1 do read(b.a[i]);
      for i:=n-1 downto 1 do
      begin
         a.a[i]:=(a.a[i] xor a.a[i+1]);
      end;

      for i:=n-1 downto 1 do
         b.a[i]:=b.a[i] xor b.a[i+1];
      a.len:=n;
      b.len:=n;

      subtract(a,b,c);
//      for i:=n downto 1 do write(c.a[i]);
//      writeln;
      fillchar(d,sizeof(d),0);
      d.len:=1;
      for i:=n downto 1 do
         if c.a[i]>0 then add(node2[i],d,d);

      for i:=d.len downto 1 do write(d.a[i]);


      writeln;
      dec(m);
   end;
{   close(input);
   close(output);
}
end.


展开阅读全文

没有更多推荐了,返回首页