现在的位置: 首页 > 综合 > 正文

USACO 2.2.3 Runaround Numbers解题报告

2013年02月19日 ⁄ 综合 ⁄ 共 2514字 ⁄ 字号 评论关闭

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
题目大意就是给出一个M,求比M大得最小的一个“循环数”
  • 如果你从最左边的数字开始 ( 在这个例子中是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 这是你回到了起点, 在从每一个数字开始数1次之后. 如果你在从每一个数字开始数一次以后没有回到起点, 你的数字不是一个循环数。

这题可以从M开始,一个个枚举数据,知道遇到循环数,没什么技术含量

 1 /*
2 ID:shiryuw1
3 PROG:runround
4 LANG:C++
5 */
6 #include<iostream>
7 #include<cstdlib>
8 #include<cstdio>
9 #include<cstring>
10 #include<algorithm>
11 #include<cmath>
12 using namespace std;
13 bool isrod(unsigned m)
14 {
15 unsigned t=m;
16 bool num[10]={false};
17 char str[20]={0};
18 sprintf(str,"%u",m);
19 char ch=str[0];
20 char len=strlen(str);
21 int k=0;
22 num[str[0]-'0']=true;
23 k=(k+(str[0]-'0'))%len;
24 while(k)
25 {
26 if(num[str[k]-'0'])
27 {
28 return false;
29 }
30 num[str[k]-'0']=true;
31 k=(k+(str[k]-'0'))%len;
32 }
33 int i,sum=0;
34 for(i=0;i<10;i++)
35 sum+=num[i];
36 if(sum<((int)log10(1.0*m)+1))
37 return false;
38 return true;
39 }
40 int main()
41 {
42 freopen("runround.in","r",stdin);
43 freopen("runround.out","w",stdout);
44 unsigned m;
45 scanf("%u",&m);
46 m++;
47 while(m)
48 {
49 bool tag=false;
50 unsigned t=m;
51 unsigned num[10]={0};
52 unsigned k=1
53 while(t)
54 {
55
56 int s=t%10;
57 if(s==0)
58 {
59 m+=k;
60 tag=true;
61 break;
62 }
63 if(num[s]!=0)
64 {
65 m+=num[s];
66 tag=true;;
67 break;
68 }
69 num[s]=k;
70 k*=10;
71 t/=10;
72 }
73 if(tag)
74 {
75 continue;
76 }
77 if(isrod(m)==true)
78 {
79 printf("%u\n",m);
80 break;
81 }
82 else
83 m++;
84 }
85 return 0;
86 }
 

抱歉!评论已关闭.