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

 

    题意:

    给出一个数 N,找大于 N 的最小一个数,满足以下条件:

    1.这个数的每一位组成都是不相同的数;

    2.组成这个数的数不包含 0;

    3.按要求向右数数(数完再从第一位开始),向右数的个数为每次数完后的最末位的数,以每位数为起点都能回到自己本身。

    

    思路:

    枚举。

    因为每一位都要求是不一样的,故最多也只可能出现 9 位,故从给出的数 N 开始 + 1 进行判断,对数数的方法进行模拟,方便数数并且标记,所以用 char 型数组存,以每个数为起点各数一次,保存每次数完后的结尾数,若这些结尾数构成的数刚好都包括了这个数的每一位数,则满足要求。

   

    AC:

/*    
TASK:runround   
LANG:C++    
ID:sum-g1    
*/
#include<stdio.h>
#include<string.h>
char num[50],Cout[5],sum[50];
int temp[50],vis[10],e[10];

int test1()  //测试是否出现0且是否没有相同的数
{
    memset(vis,0,sizeof(vis));
    int len = strlen(sum);
    for(int i = 0;i < len;i++)
    {
        if(vis[sum[i] - '0']) return 0;
        if(sum[i] == '0')     return 0;
        vis[sum[i] - '0'] = 1;
    }
    return 1;
}

int test2()  //测试每一位数是否都能在末尾出现过
{
    for(int i = 0;i < 10;i++)
    {
        if(vis[i])
        {
            if(!e[i]) return 0;
        }
    }
    return 1;
}

void plus_one()  //对数进行+1处理
{
    int len = strlen(num);
    int CIN = 0;
    for(int i = len - 1;i >= 0;i--)
    {
        int ans;
        if(i == len - 1)    ans = num[i] - '0' + 1 + CIN;
        else                ans = num[i] - '0' + CIN;
        num[i] = ans % 10 + '0';
        CIN = ans / 10;
    }
    int len_out = 0;
    while(CIN)
    {
        Cout[len_out++] = CIN % 10 + '0';
        CIN /= 10;
    }
    int k = 0;
    len_out = strlen(Cout);
    for(int i = len_out - 1;i >= 0;i--)
        sum[k++] = Cout[i];
    for(int i = 0;i < len;i++)
        sum[k++] = num[i];
    sum[k] = '\0';
    return;
}


int main()
{
    freopen("runround.in","r",stdin);        
    freopen("runround.out","w",stdout);
    int k,len,sta,end;
    scanf("%s",num);
    while(1)
    {
        do
        {
            plus_one();
        }while(!test1());
        len = strlen(sum);
        memset(temp,0,sizeof(temp));
        memset(e,0,sizeof(e));
        k = 0;
        sta = 0;
        while(!temp[sta])   //统计末尾的数
        {
            temp[sta] = 1;
            int ans = sum[sta] - '0';
            while(ans > len)           ans -= len;
            if(ans > len - (sta + 1))  end = ans - (len - sta - 1) - 1;
            else                       end = sta + ans;
            e[sum[end] - '0'] = 1;
            if(temp[end]) break;
            sta = end;
        }
        if(test2())
        {
            puts(sum);
            break;
        }
    }
    return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值