java 寻找连续出现的数字

还是学校的数据结构的小作业

先来看题,

/**
	 * Returns true iff the vector passed as argument contains at least 
	 * the required number of occurrences of the integer passed as argument.
	 * It returns false otherwise.
	 * 
	 * @param  number_to_find  the number we are looking for in the vector
	 * @param  num_of_occs     the minimum number of consecutive occurrences of number_to_find
	 * @param  where_to_find   the vector where number_to_find is to be sought for 
	 * @return true if the number is found consecutively the stated number of times 
	 */	

给定数组(where_to_find),给定要寻找的数字(number_to_find) 和 该数字出现的次数(num_of_occs),

若数组中该数字实际出现的次数至少为给定的数字出现的次数,则返回true,反之 false.

public boolean n_integers(int number_to_find, int num_of_occs, int where_to_find[])


n integers(1, 2, {1,2,3,3,3})    -------->    false
n integers(3, 2, {1,2,3,3,3})    -------->    true
n integers(3, 3, {1,2,3,3,3})    -------->    true
n integers(3, 4, {1,2,3,3,3})    -------->    false


package n_Ints;

public class Check_consecutive_integers {

	
	public boolean n_integers(int number_to_find, int num_of_occs, int where_to_find[]) {

		int n = 0;
		
		if(where_to_find.length

附上Tester

package n_Ints;

import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.lang.management.ManagementFactory;


public class Tester_1 {
	static boolean correct = true;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		try {
		      String[] ids = ManagementFactory.getRuntimeMXBean().getName()
			  .split("@");
		      BufferedWriter bw = new BufferedWriter(new FileWriter("pid"));
		      bw.write(ids[0]);
		      bw.close();
		} catch (Exception e) {
		    System.out.println("Avisa al profesor de fallo sacando el PID");
		}

		
		int s0[] = {0,1,2,3,4,5,6};
				
		do_check(3, 1, s0,false);
		do_check(1, 0, s0,true);
		do_check(1, 1, s0,true);
		do_check(1, 6, s0,true);
		do_check(3, 7, s0,false);
		do_check(3, 0, s0,false);
		do_check(1, 7, s0,false);
		
		int s1[] = {4,4,4,4,1,2,2,6,6,6,6,6,6,3,3,3,5,5,5,5,5};
		do_check(0, 0, s1,true);
		do_check(1, 0, s1,false);
		
		do_check(1, 1, s1,true);
		do_check(2, 1 , s1,false);
		do_check(3, 1 , s1,false);
		
		do_check(0, 2, s1,true);
		do_check(1, 2, s1,true);
		do_check(2, 2, s1,true);
		do_check(3, 2, s1,false);
		do_check(4, 2, s1,false);
		
		do_check(0, 3, s1,true);
		do_check(2, 3, s1,true);
		do_check(3, 3, s1,true);
		do_check(4, 3, s1,false);
		do_check(5, 3, s1,false);
		
		do_check(0, 4, s1,true);
		do_check(1, 4, s1,true);
		do_check(2, 4, s1,true);
		do_check(3, 4, s1,true);
		do_check(4, 4, s1,true);
		do_check(5, 4, s1,false);
		do_check(6, 4, s1,false);

		do_check(0, 5, s1,true);
		do_check(1, 5, s1,true);
		do_check(2, 5, s1,true);
		do_check(3, 5, s1,true);
		do_check(4, 5, s1,true);
		do_check(5, 5, s1,true);
		do_check(6, 5, s1,false);
		do_check(7, 5, s1,false);
		
		do_check(0, 6, s1,true);
		do_check(1, 6, s1,true);
		do_check(2, 6, s1,true);
		do_check(3, 6, s1,true);
		do_check(4, 6, s1,true);
		do_check(5, 6, s1,true);
		do_check(6, 6, s1,true);
		do_check(7, 6, s1,false);
		do_check(8, 6, s1,false);
	
		do_check(0, 7, s1,true);
		do_check(1, 7, s1,false);
		do_check(2, 7, s1,false);
		do_check(3, 7, s1,false);
		do_check(4, 7, s1,false);
		do_check(5, 7, s1,false);
		do_check(6, 7, s1,false);
		do_check(7, 7, s1,false);
		do_check(8, 7, s1,false);
		
		if (correct)
			System.out.println("Test OK.");
	}
	
	static void do_check(int ocs, int num, int vec[], boolean expected) {
		Check_consecutive_integers checker = new Check_consecutive_integers();
		if (checker.n_integers(num, ocs, vec) != expected) {
			System.out.println("The array " + Arrays.toString(vec) + " does not have " + ocs + " occurence(s) of " + num);
			correct = false;
		}
	}
}






你可能感兴趣的:(算法)