USACO 2.2.3 [Runaround Numbers] (runround)

算法:模拟
又是一道模拟题,找离给出的数最近的循环数。

因为不能出现0而且不能重复,所以我们转换成字符串然后开一个bb数组记录一下,判断的时候也用一个b数组记录一下,把走过的位置标记一下,如果找到某个位置已走过,查看是不是起点,若不是就退出再往下试。

{
ID:1011mashuo
PROG:runround
LANG:PASCAL
}
program runround;

var
  m:longint;
  ch:array [0..8] of longint;
  b:array [0..8] of boolean;
  bb:array [1..9] of boolean;
  
function test(x:longint):boolean;
var
  i,j,t,n,start:longint;
  s:string;
begin
  fillchar(ch,sizeof(ch),0);
  fillchar(bb,sizeof(bb),false);
  str(x,s);
  n:=length(s);
  for i:=1 to n do
    begin
      ch[i-1]:=ord(s[i])-48;
      if ch[i-1]=0 then exit(false);
      if not bb[ch[i-1]] then bb[ch[i-1]]:=true else exit(false);
    end;
  for i:=0 to n-1 do
    begin
      fillchar(b,sizeof(b),false);
      t:=i;
      start:=i;
      while not b[t] do
        begin
          b[t]:=true;
          t:=(t+ch[t]) mod n;
        end;
      for j:=0 to n-1 do if not b[j] then exit(false);
      if t<>start then exit(false);
    end;
  exit(true);
end;
  
procedure main;
begin
  while 1=1 do
    begin
      inc(m);
      if test(m) then
        begin
          writeln(m);
          halt;
        end;
    end;
end;

begin
  assign(input,'runround.in');
  reset(input);
  assign(output,'runround.out');
  rewrite(output);

  readln(m);
  main;
  
  close(input);
  close(output);
end.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值