论坛首页 综合技术论坛

跳格子游戏

浏览 1239 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-04-18  
//富老师玩跳格游戏,规则如下:从起点开始,每次可以向前跳最多k个格,
//每个格子中有0到n的数字,如果富老师所在的格子中有数字i,
//则下一次富老师最多跳k+i个格,问富老师最少几次可以跳到终点。
//例如:下图所示的情况,k=3时,富老师最少4次跳到终点。
//S	0	0	2	1	0	0	3	0	1	0	0	0	0	E

public class Title25 {
	public static void main(String[] args){
		f();
	}
	//
	public static void f(){
		int[] a = {0,0,10,10,0,0,3,0,1,0,0,0};
		int k = 3;
		
		//规则:每次条k , 然后与当前元素相加,继续向后跳  ,求次数
		int count = 0;
		int temp = 0;
//		System.out.println("数组的长度为:"+a.length);
		int i=0;
		for(;;){
			//到达终点的条件是:次数大于数组长度
			if(temp>a.length){
				break;
			}
			i = a[i]+k;	
			count++;	//循环几次  就代表跳了几次
//			System.out.println("a[i]的值为:"+a[i]);
			temp+=i;	//记录所跳的总长度
		}
		System.out.println("count的值为:"+count);
	}
}
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics