Cup Blue Bridge fractional cycle segment length - Analog division Java

Ideas: Find circulation section (composed by Yu number)
because in a cycle section, there may be duplicate numbers, such as 3/17, can not be analyzed according to figures after the decimal point. This question is whether the remainder is equal to the previously calculated every time by looking to determine the length of the loop section. Because once the remainder equally, times ten divided by the divisor must also be equal.

import java.util.Vector;
import java.util.Scanner;
class Main {
	public static void main(String[] args) {//一定要是publIC!!!!!
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		int m = cin.nextInt();
		System.out.println(solve(n, m));
	}
	 static int solve(int n, int m) {
		 n = n % m;	
   Vector<Integer> v = new Vector<Integer>();
   for(;;) {
	   v.add(n);
	   n *= 10;
	   n = n % m;
	  if(n == 0)
		  return 0;//m是不变的
	  else if(v.indexOf(n) >= 0)//用余数做判断标准,万无一失(因为余数 * 10 再除m得到的结果一定和上一次是一样的)。要注意循环节可能不是从0开始的所以要用size - indexOf
		  return v.size() - v.indexOf(n);
		}
	}
}

Published 246 original articles · won praise 8 · views 5596

Guess you like

Origin blog.csdn.net/weixin_43960370/article/details/104084392