[USACO]Runaround Numbers
2012-09-26 14:48
316 查看
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.
[/code]
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, MSAMPLE 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
纠结了好半天,拼拼凑凑,缝缝补补可算是AC了。
题解:
其实就是求一个数是否为循环数:
1、有效位不能含有0;
2、不能包含重复数字,如121就是非法的;
3、循环数定义(仅限本题):从起始位开始行走,行走的步数为起始位所对应值,遇到尾则从头继续;如果遍历了全部数据且回到了起始位置,则此树为循环数。下用一个数举例:
1246895:
起始位0,值为1,从0位开始走1步,至1;
值为2,从1位开始走两步,至3;
值为6,从3位开始走6步,至2;
……
最后回到1位置,且所有数组走过且仅经过一次,说明概数为循环数。
写几个循环数行走,可以发现一个规则,关于如何快速判断下次落脚位置,若i为此次停止位即下次起始位,则下次的落脚位为:
(i+1)=(i+round[i])/round.length;
剩下的就是一些细节上的判断。
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class runround { public static void main(String[] args) throws IOException,FileNotFoundException { BufferedReader br = new BufferedReader(new FileReader("runround.in")); FileWriter fout = new FileWriter("runround.out"); String M = br.readLine(); int m = Integer.parseInt(M); boolean isRunAound = false; while(!isRunAound) { m=increase(m); M = new String(""+m); char[] chars = M.toCharArray(); int start = 0; int[] touched = new int[chars.length]; touched[0]=1; int i ; for(i = 1;i<=chars.length;i++) { start = (start+chars[start]-'0')%chars.length; if(touched[start]==1) { break; }else { touched[start]=1; } } if(i==chars.length&&start==0) { break; } } fout.write(m+"\n"); fout.flush(); fout.close(); br.close(); System.exit(0); } private static int increase(int m) { while(!check(++m)){ } return m; } private static boolean check(int i) { int[] digits = new int[10]; while(i>0) { if(i%10==0||digits[(i%10)-1]==1) { return false; } digits[(i%10)-1]=1; i/=10; } return true; } }
[/code]
相关文章推荐
- USACO Runaround Numbers 模拟
- USACO:2.2.3 Runaround Numbers 循环数
- USACO-Section 2.2 Runaround Numbers (枚举)
- USACO :Runaround Numbers 解题报告
- USACO-Section2.2 Runaround Numbers [其他][模拟]
- USACO 2.2 Runaround Numbers(乱搞)
- USACO 2.2 Runaround Numbers
- USACO2.2.3 Runaround Numbers (runround)
- USACO 2.2.3 Runaround Numbers(枚举+模拟)
- 【其他】【USACO】Runaround Numbers
- USACO 2.2 Runaround Numbers
- USACO 2.2.3 Runaround Numbers 循环数
- USACO Section2.2 Runaround Numbers 解题报告 【icedream61】
- [2016/7/26][usaco 2.2]Runaround Numbers
- usaco Runaround Numbers
- USACO-Runaround Numbers
- USACO 2.2 Runaround Numbers
- [USACO2.2]循环数 Runaround Numbers
- [USACO 2.2.3] Runaround Numbers
- usaco2.23Runaround Numbers