彩色石子

http://hero.youkuaiyun.com/?ref=toolbar里面的题目,说实话,这题没看懂怎么提交测试的吐舌头,不过思路是有的

题目分析:
就是找规律,应该玩过俄罗斯方块吧,大概就是新添加一个方块后,尽量消除相同的。
这里是添加石头,要提前添加2个石头,之后每次添加就是尽量消除相同的,并排除异己,统计排除的异己个数就ok了

下面是Java的代码,正确性有待验证:

public class Main {
	public static void main(String[] args) {
		int n, k; // 1<=n<=100, 1<=k<=5
		int[] stone = new int[6];

// Test one		
//		// beigin 输入的数据
//		n = 10;
//		k = 3;
//		int[] input = new int[] { 0, 2, 1, 2, 2, 1, 1, 3, 1, 3, 3 }; // input[0]无意义
//		// end
		
// Test two		
		// beigin 输入的数据
		n = 3;
		k = 3;
		int[] input = new int[] { 0, 1,2,3 }; // input[0]无意义
		// end

		for (int i = 0; i <= k; ++i) {
			stone[k] = 0;
		}
		int count; // 统计stone[i]=1的个数
		int index=0; // 指向tone[i]=1的位置,当count=1时有效

		int result = 0;
		stone[input[1]] += 1;
		stone[input[2]] += 1;
		for (int i = 3; i <= n; ++i) {
			stone[input[i]] += 1;
			count = 0;
			index = 0;
			for (int j = 1; j <= k; ++j) {
				if (stone[j] == 1) {
					index = j;
					count++;
				} else if (j != input[i]){ 
						stone[j] = 0;
				}// end if
			}// end for
			if (count == 1 && index!=input[i]) {
				stone[index]=0;
				result++;
			}
			
			System.out.println(stone[1]+" "+stone[2]+" "+stone[3]);
		}// end for
		
		for(int i=1;i<=k;++i){
			if(stone[i]==1){
				result++;
			}// end if
		}// end for
		
		System.out.println(result);
	}// end main
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值