Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴一下Java代码,很慢,很水

Posted by 20101289 at 2015-04-22 15:00:37 on Problem 1250
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        int capacity;
        char[] customers;
        Map<Character,String> beds;
        Map<Character,String> left;

        int count;
        Scanner sc = new Scanner(System.in);
        while(true){
            capacity = sc.nextInt();
            if(capacity==0)
                return;
            customers = sc.next().toCharArray();

            count=0;
            beds = new HashMap<Character, String>();
            left = new HashMap<Character, String>();
            for (int i = 0; i < customers.length; i++) {
                if(beds.containsKey(customers[i]))
                    beds.remove(customers[i]);
                else{
                    if(left.containsKey(customers[i]))
                        left.remove(customers[i]);
                    else if(beds.size()>=capacity){
                        count++;
                        left.put(customers[i],"");
                    } else{
                        beds.put(customers[i],"");
                    }
                }

            }
            if(count==0)
                System.out.println("All customers tanned successfully.");
            else
                System.out.println(count + " customer(s) walked away.");
        }

    }


}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator