[USACO2.2]循环数 Runaround Numbers

来源:互联网 发布:c语言hex bcd 编辑:程序博客网 时间:2024/06/11 21:50

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

Runaround Numbers循环数

目录

 [隐藏] 
  • 1 描述
  • 2 格式
  • 3 SAMPLE INPUT
  • 4 SAMPLE OUTPUT

[编辑]描述

循环数是这样的整数:它包含的数字都是独特不相同的,(如1111就是不正确的),而且没有0,例如81362。它有一个有趣的性质:

1.从左端开始,当前的数是多少就往右数几位(首尾相接,即认为最右边的数字之后是左边第一个数),对于81362,你将会停在一个新数字6上

2.重复上述过程,这回数6个数字因为刚刚停在6上。你将会停在2上

3.继续,(数2个数字),停在1

4.继续,(数1个数字),停在3

5.停在8,这个时候你已经接触了每个数字一次且仅一次。如果不是这样,那就不是循环数。

给定一个数M,找到并输出刚好比M大的下个循环数。使用unsigned long存储M

[编辑]格式

PROGRAM NAME: runround

INPUT FORMAT:

(file runround.in)

仅仅一行, 包括M

OUTPUT FORMAT:

(file runround.out)

仅仅一行,输出第一个比M大的循环数。

[编辑]SAMPLE INPUT

81361

[编辑]SAMPLE OUTPUT

81362



【题解】模拟还是模拟~姿势在代码里




/*ID:luojiny1LANG:C++TASK:runround*/#include<cstdio>int a[34]={0},n=0;bool ok(){/******************/bool c[10]={0},flag=0;for(int j=0;j<n;j++)if(c[a[j]]==0&&a[j]!=0)c[a[j]]=1;else {flag=1;break;}if(flag)//continue;return false;/*以上按照题意判重和零*/bool vis[34]={0};//记录第i位是否访问过 vis[n-1]=1;//从第一个数字开始 for(int i=n-1;;)//模拟数数字 {int de=a[i]%n;    if(i-de>=0)i-=de;else {i=i-de+n;}if(vis[i])if(i==n-1)//终点必须是第一个 break;else return false;//否则不符合 vis[i]=1;}for(int i=0;i<n;i++)if(vis[i]==0)return false;//判断每个位都访问过 return true;}int main(){freopen("runround.in","r",stdin);freopen("runround.out","w",stdout);long long  m;scanf("%lld",&m);m++;while(m)//long long转高精度 {a[n++]=m%10;m/=10;}while(ok()==0){a[0]++;int i=0;while(a[i]>=10){//进位 a[i]%=10;a[i+1]+=1;if(i==n-1)n++;i++;}}for(int i=n-1;i>=0;i--)printf("%d",a[i]);printf("\n");return 0;}












原创粉丝点击