题目描述
n 位的一个整数是循环数(cyclic)的条件是:当用一个 1 到 n 之间的整数去乘它时, 会得到一个将原来的数首尾相接循环移动若干数字再在某处断开而得到的数字。也就是说,如果把原来的数字和新的数字都首尾相接,他们得到的环是相同的。只是两个数的起始数字不一定相同。例如,数字 142857 是循环数,因为:
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142
输入
写一个程序确定给定的数是否是循环数。输入包括多个长度为 2 位到 60 位的整数。(注意,先导的0也是合理的输入不应该被忽略,例如 "01"是 2 位数,“1” 是 1 位数。)
输出
对于每一个输入的整数,输出一行表明它是否是循环数。
样例输入
142857
142856
142858
01
0588235294117647
样例输出
142857 is cyclic
142856 is not cyclic
142858 is not cyclic
01 is not cyclic
0588235294117647 is cyclic
解题代码:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String num_str;
while(in.hasNextLine())
{
num_str = in.nextLine();
if(fornum(num_str) == true)
System.out.println(num_str + " is cyclic");
else
System.out.println(num_str + " is not cyclic");
}
}
public static boolean fornum(String str)
{
int len = str.length();
String b = String.valueOf(len);
BigInteger num = new BigInteger(str);
BigInteger mul = num.multiply(new BigInteger(b));
String mul_str = mul.toString();
if(mul_str.length() == len)
{
for (int i = 0; i < len; i++)
if(mul_str.indexOf(str.charAt(i)) == -1)
return false;
}else
{
return false;
}
return true;
}
}