zoukankan      html  css  js  c++  java
  • USACO2.2.3Runaround 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
    

     题解:就是赤果果的模拟,不过我提交上去居然WA了,原因是我数组长度只开到9。。。最大数字只到8。。。以后数组果断开稍微大一点。。。这样可以避免数组越界。。。

    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:runround
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 int maxlen;
     8 int check(long m)
     9 {
    10     int a[10];
    11     memset(a,0,sizeof(a));
    12     maxlen=0;
    13     while(m)
    14     {
    15         maxlen++;
    16         if(m%10==0||a[m%10])return 0;
    17         a[m%10]=1;
    18         m/=10;
    19     }
    20     return 1;
    21 }
    22 int main(void)
    23 {
    24     freopen("runround.in","r",stdin);
    25     freopen("runround.out","w",stdout);
    26     int a[10],f[10];
    27     long i,j,k,m,len,flag,mm;
    28     scanf("%ld",&m);
    29     flag=1;
    30     while(flag)
    31     {
    32         m++;
    33         if(!check(m)) continue;
    34         len=0;
    35         memset(f,0,sizeof(f));
    36         mm=m;
    37         while(mm)
    38         {
    39             len++;
    40             a[maxlen-len+1]=mm%10;
    41             mm/=10;
    42         }
    43         j=len;
    44         i=1;
    45         while(j)
    46         {
    47             k=(a[i]+i-1)%len+1;
    48             if(f[a[k]]) break;
    49             f[a[k]]=1;
    50             i=k;
    51             j--;
    52         }
    53         if(j==0)
    54         {
    55             printf("%ld\n",m);
    56             flag=0;
    57         }
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    网络系列之
    网络系列之
    Linux命令系列之
    Linux命令系列之
    Linux命令系列之
    Linux命令系列之
    Linux命令系列之
    Linux命令系列之
    Linux命令系列之
    Linux命令系列之
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2902922.html
Copyright © 2011-2022 走看看