首页 > 专题系列 > Java解POJ > POJ 2551 Ones [解题报告] Java
2013
11-11

POJ 2551 Ones [解题报告] Java

Ones

问题描述 :

Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1's. How many digits are in the smallest such a multiple of n?

输入:

Each line contains a number n.

输出:

Output the number of digits.

样例输入:

3 
7 
9901

样例输出:

3
6
12

解题代码:

//* @author 洪晓鹏<[email protected]>
import java.util.Scanner;


public class Main {
 public static void main(String[] args)
 {
  Scanner in = new Scanner(System.in);
  while(in.hasNext())
  {
	int input = in.nextInt();
	int result = 1;
	int begin = 1;
    while(true)
	{
	if(begin % input ==0)
		{
			System.out.println(result);
			break;
		}
	begin = begin%input;
	begin = begin*10 + 1;
	result ++;
    }
   }
 }
}

  1. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。