[Daily Blue Bridge] 27. The real question of the Java Group in the 15th Provincial Competition "Length of Recurring Festival"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Length of Loop Section

Dividing two integers will sometimes produce cyclic decimals. The cyclic part is called: cyclic section, such as:

11/13=6=>0.846153846153......, the loop section is [846153], a total of 6 digits, the following method can find the length of the loop section,

Please read the code carefully and fill in the missing code in the underlined part:
public static int  f( int n , int m ) {     

n = n % m;

Vector v = new Vector();

for(;;) {

v.add(n);

n *= 10;

n = n % m;

if (n==0) return 0;

if ( v .indexOf( n )>=0) ____________________ //fill in the blanks ;

}

}

Note: Only fill in the missing part, do not copy the existing code repeatedly, and do not fill in any extra text.

Problem-solving ideas:

This question mainly uses vector to store the remainder, then compare the remainder with the data stored in the array to determine whether the data is repeated, and then determine the length of the repeated data.

Answer source code:

import java.util.Vector;

public class Year2015_Bt4 {

	public static void main(String[] args) {
		System.out.println(f(11, 13));
	}
	
	public static int f(int n,int m) {
		n = n % m;
		Vector v = new Vector();
		for(;;) {
			v.add(n);
			n *= 10;
			n = n % m;
			if (n==0) return 0;
			if(v.indexOf(n)>=0) return v.size()-v.indexOf(n);
		
		}
		
	}

}

 

Sample output:

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/114538738