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<num_of_occs)
			return false;
		
		for(int i=0; i<where_to_find.length && n<num_of_occs; i++){
			if(where_to_find[i]==number_to_find)
				n++;
			else
				n=0;			
		}
		
		return n== num_of_occs;
	}
}

附上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;
		}
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值