POJ ACM 1047

来源:互联网 发布:软件项目风险分析 编辑:程序博客网 时间:2024/06/09 16:45

   这道题目折腾了我很久很久,最主要的问题就在于这个数字要求是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

142857142856142858010588235294117647

Sample Output

142857 is cyclic142856 is not cyclic142858 is not cyclic01 is not cyclic0588235294117647 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也是个半吊子,哎~继续努力努力!!!

原创粉丝点击