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
}