问题描述:
有一张靠墙的桌子,只有一面可以坐人,现在坐人的位置是1,不坐人的位置是0,现在有一些位置已经被占了,后来的人不想和任何人相邻(包括后来的人和原先坐在那里的人),请问有对于一个输入序列(如10001),判断可不可以再坐n个人。
AC代码:
package hello;
import java.util.Scanner;
public class Xiaomi{
static boolean fun(String table, int n) {
int[] a = new int[table.length()+4];
for(int i = 0; i < table.length(); i++)
{
a[i+2] = Integer.parseInt( String.valueOf(table.charAt(i)));
}
//两侧
a[0]=1;
a[1]=0;
a[a.length-2]=0;
a[a.length-1]=1;
//两侧
int m=0;
//boolean ison=false;
int count=0;
for(int i=0;i<a.length;i++) {
if(a[i]==0) {
// ison=true;
count+=1;
}
if(a[i]==1) {
m=m+(count-1)/2;
// ison=false;
count=0;
}
}
if(m<n) {
return false;
}else {
return true;
}
}
public static void main(String []args) {
Scanner scanner =new Scanner(System.in);
String table=scanner.nextLine();
int n=scanner.nextInt();
boolean result=fun(table,n);
if(result) {
System.out.print("yes");
}else {
System.out.print("no");
}
}
}