您的位置:首页 > 其它

【其他】【USACO】Runaround Numbers

2010-06-11 07:33 323 查看
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

描述

循环数是那些不包括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 这时你回到了起点,在经过每个数字一次后回到起点的就是循环数。如果你经过每一个数字一次以后没有回到起点, 你的数字不是一个循环数。

给你一个数字 M (在1到9位之间), 找出第一个比 M大的循环数, 输出数据保证结果能用一个无符号长整型数装下。

格式

PROGRAM NAME: runround

INPUT FORMAT:

(file runround.in)

仅仅一行, 包括M

OUTPUT FORMAT:

(file runround.out)

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

SAMPLE INPUT

81361

SAMPLE OUTPUT

81362
来自"http://www.nocow.cn/index.php/Translate:USACO/runround"

最开始数组顺序问题……然后是 %len的问题 一次AC

/*
ID:sephiro5
LANG:C++
PROG:runround
*/
#include<stdio.h>
bool v[10],ha[10];
int a[100],len;
bool complete(){
for (int i=0;i<=9;++i)
if (ha[i]&&!v[i]) return false;
return true;
}
void swap(int &a,int &b){
int t;
t=a; a=b; b=t;
return;
}
bool check(int x){
for (int i=0;i<=9;++i)
v[i]=ha[i]=false;
int te=x,tem=x;
len=0;
while (te!=0){
tem=te%10;
a[++len]=tem;
te/=10;
}
if (len&1==1)
for (int i=1;i<=len/2+1;++i)
swap(a[i],a[len-i+1]);
else for (int i=1;i<=len/2;++i)
swap(a[i],a[len-i+1]);
int now=1,pos;
for (int k=1;k<=len;++k){
pos=now+a[now]; pos%=len; if (pos==0) pos=len;
if (v[a[pos]]) return false;
v[a[pos]]=true;
now=pos;
}
if (complete()) return true;
return false;
}
int main(){
freopen("runround.in","r",stdin);
freopen("runround.out","w",stdout);
int n;
scanf("%d",&n);
while (1){
++n;
if (check(n)) break;
}
printf("%d/n",n);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: