這道題目折騰了我很久很久,最主要的問題就在于這個數字要求是64位,在C++中又不這麼會用。後來終于用java中的BigInteger解決了問題。下面先看看題目:
Description
A cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a"cycle"of the digits of the original number. That is, if you consider the number after the last digit to "wrap around"back to the first digit, the sequence of digits in both numbers will be the same, though they may start at different positions.For example, the number 142857 is cyclic, as illustrated by the following table:
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142
Input
Write a program which will determine whether or not numbers are cyclic. The input file is a list of integers from 2 to 60 digits in length. (Note that preceding zeros should not be removed, they are considered part of the number and count in determining n. Thus, "01"is a two-digit number, distinct from "1" which is a one-digit number.)
Output
For each input integer, write a line in the output indicating whether or not it is cyclic.
Sample Input
142857
142856
142858
01
0588235294117647
Sample Output
142857 is cyclic
142856 is not cyclic
142858 is not cyclic
01 is not cyclic
0588235294117647 is cyclic
主要運用的是一種循環:
1´ 142857=142857
2´ 142857=285714
3´ 142857=428571
4´ 142857=571428
5´ 142857=714285
6´ 142857=857142
所得得結果是1、4、2、8、5、7六個數字依原來的次序循環排列隻是開頭的數字變動而已。
這種數我們叫做循環數,其實這個數142857是由1/7所形成循環小數的循環節(1/7=0.142857142857142857…)。
而所有循環數也都由某質數的倒數所形成之循環小數的循環節而得來的。下一個循環數是由質數17所形成的,
1/17=0.0588235294117647…,而0588235294117647即為一循環數(2? 0588235294117647=1176470588235294)。
會産生如此循環數的質數依序為7、17、19、23、29、47、59、61、97(<100)。
142857還有一個很有趣的性質。當142857乘以7時其乘積為一連串的9(142857´ 7=999999),
而0588235294117647乘以17也是一連串的9。還有142857分成兩半相加也是一連串的9(注:142+857=999),
而0588235294117647分成兩半相加: 05882352+ 94117647=99999999,這真是非常奇妙的巧合。(這段話是截取别人的分析)
下面是我的代碼:
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
BigInteger number ; //輸入的數字
String original ; //輸入數字的字元形式
String r ; // '9999····'
String m ; //乘數的字元形式
String resul ; //相乘結果的字元形式
BigInteger multiNumber ; //乘數
BigInteger result ; //乘積
int length = 0 ;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
original = cin.next();
length = original.length() ;
number = new BigInteger(original) ;
char []dist = new char[length];
for(int i = 0 ; i < length ;i++)
{
dist[i] = '9' ;
}
r = new String(dist);
m = String.valueOf(length+1);
multiNumber = new BigInteger(m);
result = number.multiply(multiNumber);
resul = String.valueOf(result) ;
if(resul.endsWith(r))
{
System.out.println(original+" "+"is cyclic");
}
else
System.out.println(original+" "+"is not cyclic");
}
}
}
其實通過這個題目我發現了自己存在的很多問題,對于C++的運用不是很熟練,對于java也是個半吊子,哎~繼續努力努力!!!