java
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int count = 0;
int res = 0;
for(int i = 0; i < num; i++){
int n = in .nextInt();
if(n == 0){
count++;
} else {
if(count > 0){
res += (count - 1) / 2;
}
count = 0;
}
}
if(count != 0) {
res += count / 2;
}
int tar = in.nextInt();
if(tar > res){
System.out.println(false);
} else {
System.out.println(true);
}
}
}
描述
给定一个数组 arrayarray ,数组中只包含 11 和 00 ,且数组中的 11 都不相邻。
输入一个数 nn ,问能否在将数组中 nn 个 00 替代换成 11 后不破坏 11 都不相邻的条件。
例1 array=1,0,0,0,1,n=1 输出 true
例2 array=1,0,0,0,1,n=2 输出 false
输入描述:
输入一个数 m(1≤m≤105)m(1≤m≤105) 表示 arrayarray 的长度
第二行 mm 个数 00 或 11 表示该位置数组中的元素是 00 还是 11
第三行输入一个数 nn
输出描述:
输出 true 或 false
思路:
1、统计1和1之间0的个数;
2、3 -> 1 2->0 4->1 5->2,可修改数量为中间0的个数-1然后除以2;
3、需要考虑末尾不是1的情况,这种直接用0的个数除2就好了;