首页 > 专题系列 > Java解POJ > POJ 1047 Round and Round We Go [解题报告] Java
2013
11-09

POJ 1047 Round and Round We Go [解题报告] Java

Round and Round We Go

问题描述 :

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

输入:

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.)

输出:

For each input integer, write a line in the output indicating whether or not it is cyclic.

样例输入:

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.util.*;
import java.math.*;
public class Main {


 public static void main(String[] args) {
  Scanner cin  = new Scanner(System.in);
  BigInteger b,c,d;
  String str,str1,str2,str3;
  int i,len;
  while(cin.hasNext())
  {
   str = cin.next();
   b = new BigInteger(str);
   len=str.length()+1;
      char []kids = new char[len-1];
      for(i=0;i< len-1;i++)
       kids[i]='9';
      str3 = new String(kids);
   str1 = String.valueOf(len);
   c = new BigInteger(str1);
   d = b.multiply(c);
   str2 = d.toString();
   if(str2.compareTo(str3)==0)
         System.out.println(str+" "+"is cyclic");
   else
    
    System.out.println(str+" "+"is not cyclic");
    
  }
  

 }

}

  1. 思路二可以用一个长度为k的队列来实现,入队后判断下队尾元素的next指针是否为空,若为空,则出队指针即为所求。