首页 > ACM题库 > HDU-杭电 > HDU 1313 Round and Round We Go-字符串-[解题报告] java
2013
12-09

HDU 1313 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 ~{!0~}cycle~{!1~} of the digits of the original number. That is, if you consider the number after the last digit to ~{!0~}wrap around~{!1~} 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, ~{!0~}01~{!1~} is a two-digit number, distinct from ~{!0~}1~{!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 

刚刚学Java  A了一题 

代码贴上来,写的不够规范。。    

import java.math.*;
import java.util.Scanner;
import java.io.*;

public class Main{
	
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		while(in.hasNext()){
		 String str=in.next();
	          int len=str.length();
		 BigInteger a=new BigInteger(str);
		 boolean flag=true;
		 for(int i=1;i<=len;i++)
		 {
			 BigInteger b=BigInteger.valueOf(i);
			 b=b.multiply(a);
			 String  str2=b.toString();
			 while(str2.length()<len)
			 {
				str2="0"+str2;
			}
			 str2=str2+str2;  //把两个字符串连在一起 判断是否是子串
			  if(str2.indexOf(str)<0)
			 { 
			
				 flag=false;
			 }
				  
			 if(flag==false) break;
			 
		 }
		 if(flag==false)
			 System.out.println(str+" is not cyclic");
		 else System.out.println(str+" is cyclic");
		 }	
	}
	
	
}

 

解题报告转自:http://blog.csdn.net/sooowhat/article/details/6655335