[USACO 2.2.3] Runaround Numbers

[题目描述]

Runaround Numbers

循环数

循环数是那些不包括0这个数字的没有重复数字的整数 (比如说, 81362) 并且同时具有一个有趣的性质就像这个例子:

  • 如果你从最左边的数字开始 在这个例子中是8) 数最左边这个数字个数字到右边(回到最左边如果数到了最右边),你会停止在另一个新的数字(如果没有停在一个不同的数字上,这个数就不是循环数). 就像: 8 1 3 6 2 从最左边接下去数8个数字: 1 3 6 2 8 1 3 6 所以下一个数字是6.
  • 重复这样做 (这次从“6”开始数6个数字并且你会停止在一个新的数字上: 2 8 1 3 6 2, 也就是2.
  • 再这样做 (这次数两个): 8 1
  • 再一次 (这次一个): 3
  • 又一次: 6 2 8 这是你回到了起点在从每一个数字开始数1次之后如果你在从每一个数字开始数一次以后没有回到起点你的数字不是一个循环数。

给你一个数字 M (19位之间), 找出第一个比 M大的循环数并且一定能用一个无符号长整形数装下。

PROGRAM NAME: runround

INPUT FORMAT

仅仅一行包括M

SAMPLE INPUT (file runround.in)

81361

OUTPUT FORMAT

仅仅一行,包括第一个比M大的循环数。

SAMPLE OUTPUT (file runround.out)

81362


[解题思路]

简单模拟,顺序枚举+判断即可。


[Code]

{
ID: zane2951
PROG: runround
LANG: PASCAL
}

program runround;
var
   v:array[0..11] of boolean;
   n,i,x,ce:longint;
   ko:boolean;

//---------sure------------
function sure(len:longint):boolean;
var
   i:longint;

begin
   for i:=1 to len do if not v[i] then exit(false);
   exit(true);
end;

//---------check-----------
function check(x:longint):boolean;
var
   s:string;
   len,now:longint;

begin
   str(x,s);
   len:=length(s);
   now:=1;
   while true do
      begin
         v[now]:=true;
         now:=(now+ord(s[now])-ord('0')) mod len;
         if now=0 then now:=len;
         if v[now] and (now<>1) then exit(false);
         if now=1 then
            if sure(len) then exit(true) else exit(false);
      end;
end;

//----------main-----------
begin
   assign(input,'runround.in'); reset(input);
   assign(output,'runround.out'); rewrite(output);
   readln(n);
   for i:=n+1 to maxlongint do
      begin
         ko:=true;
         fillchar(v,sizeof(v),0);
         x:=i;
         while x>0 do
            begin
               ce:=x mod 10;
               if v[ce] then begin ko:=false; break; end else v[ce]:=true;
               x:=x div 10;
            end;
         fillchar(v,sizeof(v),0);
         if ko then
            if check(i) then begin writeln(i); break; end;
      end;
   close(input); close(output);
end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值