题解 | #公司食堂#

公司食堂

http://www.nowcoder.com/questionTerminal/601815bea5544f389bcd20fb5ebca6a8

双指针
最后一个无法通过 卡输出

StringBuilder sb = new StringBuilder();
sb.append(输出的字符串)
System.out.print(sb);
要比println 输出效率高太多

import java.io.*;

import java.util.Scanner;
import java.util.*;


public class Main{


    public static void main(String[] args) {



        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        int t = sc.nextInt();

        for(int k=0;k<t;k++){
            int n = sc.nextInt();
            sc.nextLine();
            // 每桌的人数 table
            int[] table = new int[n];
            String table_char = sc.nextLine();
            for(int i=0;i<n;i++){
                table[i] = table_char.charAt(i)-'0';
            }
            int m = sc.nextInt();
            sc.nextLine();
            //排队 序列 que
            String que = sc.nextLine();

            int pointM = 0; //指向1座位
            int pointF = 0; //指向0座位

            for(int i=0;i<m;i++){
                if (que.charAt(i) == 'F'){
                    //寻找0,1座位
                    while(pointF<n && table[pointF]!=0 ) pointF++;
                    while(pointM < n && table[pointM]!=1) pointM++;
                    //没有0座位
                    if(pointF >= n) {
                        table[pointM]++;
                        System.out.println(pointM+1);
                    }
                    //有0座位
                    else {
                        System.out.println(pointF+1);
                        table[pointF]++;
                        pointM = Math.min(pointM,pointF);
                    }
                }else{
                    //寻找0,1座位
                    while(pointM < n && table[pointM]!=1) pointM++;
                    while(pointF<n && table[pointF]!=0 ) pointF++;
                    //没有一个人的
                    if(pointM >= n) {
                        System.out.println(pointF+1);
                        table[pointF]++;
                        pointM = Math.min(pointM,pointF); //这里是取最小的 女性会增加1个座位的
                        //有1座位的
                    }else {
                        System.out.println(pointM+1);
                        table[pointM]++;
                    }
                }
            }
        }
    }
}

卡输出 每次 println 太费时间 输出改成buffer


import java.util.Scanner;
import java.util.*;
import java.io.*;


public class Main{


    public static void main(String[] args) {


        Scanner sc = new Scanner(new BufferedInputStream(System.in));

        int t = sc.nextInt();

        for(int k=0;k<t;k++){
            int n = sc.nextInt();
            sc.nextLine();
            // 每桌的人数 table
            int[] table = new int[n];
            String table_char = sc.nextLine();
            for(int i=0;i<n;i++){
                table[i] = table_char.charAt(i)-'0';
            }
            int m = sc.nextInt();
            sc.nextLine();
            //排队 序列 que
            String que = sc.nextLine();



            int pointM = 0; //指向1座位
            int pointF = 0; //指向0座位

            // 修改输出
            StringBuilder sb = new StringBuilder();

            for(int i=0;i<m;i++){
                int seatsnum;
                if (que.charAt(i) == 'F'){
                    //寻找0座位
                    while(pointF<n && table[pointF]!=0 ) pointF++;
                    //没有0座位
                    if(pointF >= n) {
                        while(pointM < n && table[pointM]!=1) pointM++;
                        table[pointM]++;
                        seatsnum = pointM+1;
                    }
                    //有0座位
                    else {
                        seatsnum = pointF+1;
                        table[pointF]++;
                        pointM = Math.min(pointM,pointF);
                    }
                }else{
                    //寻找1座位
                    while(pointM < n && table[pointM]!=1) pointM++;
                    //没有一个人的
                    if(pointM >= n) {
                        while(pointF<n && table[pointF]!=0 ) pointF++;
                        seatsnum = pointF+1;
                        table[pointF]++;
                        pointM = Math.min(pointM,pointF);
                        //有1座位的
                    }else {
                        seatsnum = pointM+1;
                        table[pointM]++;
                    }
                }
                sb.append(seatsnum).append("\n");
            }
            System.out.print(sb);
        }
    }
}

试卷解析 文章被收录于专栏

解析

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务