zoukankan      html  css  js  c++  java
  • 2.2.3 Runaround Numbers

    Runaround Numbers

    Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration:

    • If you start at the left digit (8 in our number) and count that number of digits to the right (wrapping back to the first digit when no digits on the right are available), you'll end up at a new digit (a number which does not end up at a new digit is not a Runaround Number). Consider: 8 1 3 6 2 which cycles through eight digits: 1 3 6 2 8 1 3 6 so the next digit is 6.
    • Repeat this cycle (this time for the six counts designed by the `6') and you should end on a new digit: 2 8 1 3 6 2, namely 2.
    • Repeat again (two digits this time): 8 1
    • Continue again (one digit this time): 3
    • One more time: 6 2 8 and you have ended up back where you started, after touching each digit once. If you don't end up back where you started after touching each digit once, your number is not a Runaround number.

    Given a number M (that has anywhere from 1 through 9 digits), find and print the next runaround number higher than M, which will always fit into an unsigned long integer for the given test data.

    PROGRAM NAME: runround

    INPUT FORMAT

    A single line with a single integer, M

    SAMPLE INPUT (file runround.in)

    81361
    

    OUTPUT FORMAT

    A single line containing the next runaround number higher than the input value, M.

    SAMPLE OUTPUT (file runround.out)

    81362
    
    {
    ID: makeeca1
    PROG: runround
    LANG: PASCAL
    }
    program runround;
    var n:qword;
        bool:array[0..12]of boolean;
    function check(x:longint):boolean;
    var s:string;i,m,k,l:longint;
    begin
      fillchar(bool,sizeof(bool),0);
      str(x,s);l:=length(s);
      for i:=1 to l do
        if s[i]='0' then exit(false) else
          if bool[ord(s[i])-48]then exit(false)else
            bool[ord(s[i])-48]:=true;//ÅжÏÊÇ·ñÓÐÖØžŽÊý×Ö
      fillchar(bool,sizeof(bool),1);
      bool[1]:=false; m:=1;k:=1;
      repeat
        inc(k);//ÒÑÅжϹýÊý×ÖÊýÄ¿
        m:=(m+ord(s[m])-48) mod l;//ÖžÕë
        if m=0 then m:=l;//±ßœçŽŠÀí
        if bool[m] then bool[m]:=false else if (k<>l)and(m<>1)then exit(false);
      until k=l+1;
      if m=1 then exit(true)else exit(false);
    end;
    begin
      assign(input,'runround.in');reset(input);
      assign(output,'runround.out');rewrite(output);
      readln(n);close(input);
      repeat
        inc(n);
      until check(n);
      writeln(n);
      close(output);
    end.
  • 相关阅读:
    url 中非法字符替换,java 正则替换
    Ubuntu 下用命令行快速打开html,mp3等文件
    JavaScript HTML DOM 入门详解
    JavaScript 表单验证入门
    javascript with关键字简单用法
    JavaScript 错误处理, Throw、Try 和 Catch入门
    使用 Navicat Premium 将 sql server 的数据库迁移到 mysql 的数据库中
    引入 ServletContextListener @Autowired null 解决办法
    tomcat启动完成执行 某个方法 定时任务(Spring)
    linux启动tomcat很久或者很慢Tomcat启动时卡在“INFO: Deploying web application directory ......”的解决方法
  • 原文地址:https://www.cnblogs.com/makeecat/p/3274573.html
Copyright © 2011-2022 走看看