2017/9/18小米机试题(桌子坐人问题)

问题描述:
有一张靠墙的桌子,只有一面可以坐人,现在坐人的位置是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");
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值