PAT 1016_C++已过

全错!

 

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Comparator;
 
public class Main{
	public static void main(String [] args) throws IOException{
		Scanner in = new Scanner(new File("./in.txt"));
//		Scanner in = new Scanner(System.in);
		
		int i, n;
		int [] p = new int[24];		//cents/minute
		
		for(i = 0; i < 24; i++){
			p[i] = in.nextInt();
		}
		
		n = in.nextInt();
		
		String month = null;//For each test case, all dates will be within a single month
		
		//PriorityQueue only assure the head is the smallest, can't assure all elements are in ascending order! 
		
		PriorityQueue<String> Qnames = new  PriorityQueue<String>();	//for customer name
		 																	
		Comparator<String> cmpCall = new Comparator<String>(){   //compare record by time
			public int compare(String s1, String s2){
//				String d1 = s1.substring(3,5);		//day
//				String d2 = s2.substring(3,5);		
//				String h1 = s1.substring(6,8);		//hour
//				String h2 = s2.substring(6,8);
//				String m1 = s1.substring(9,11);		//min
//				String m2 = s2.substring(9,11);
//	
//				if(d1.compareTo(d2) != 0){
//					return d1.compareTo(d2);
//				}else if(h1.compareTo(h2) != 0){
//					return h1.compareTo(h2);
//				}else if(m1.compareTo(m2) != 0){
//					return m1.compareTo(m2);
//				}
//				return 0;
				return s1.substring(3,11).compareTo(s2.substring(3,11));
			}	
		};
		HashMap<String, PriorityQueue<String>> callRecords = new HashMap<String, PriorityQueue<String>>();//customer
																					// mapping with all his call records		 
		while(n-- > 0){				//put all n records into callRecords
									//so they are sorted by customer name and time respectively
			String name = in.next();
			String time = in.next();
			String type = in.next();
			
			  	
			if(callRecords.containsKey(name)){
				callRecords.get(name).add(time+type);				
			}else{						
				Qnames.add(name);		//add customer name		
				
				if(month == null){
					month = time.substring(0,2);
				}
				
				PriorityQueue<String> Q = new PriorityQueue<String>(11,cmpCall); 	
				Q.add(time+type);			
				callRecords.put(name , Q);
			}
			
		}
		
		//test
//		while(!Qnames.isEmpty()){
//			String name = Qnames.poll();
//			System.out.println(name+" "+month);
//			
//			PriorityQueue<String> Q = callRecords.get(name);			
//			while(!Q.isEmpty()){
//				System.out.println(Q.poll());
//			}
//		}
		while(!Qnames.isEmpty()){
			String name = Qnames.poll();
			
			StringBuilder res = new StringBuilder();//if no bill, nothing output even the customer name!
			res.append(name+" "+month+"\n");//print name and month
			
			
			
			PriorityQueue<String> Q = callRecords.get(name);
			List<String> lsCallTime = new ArrayList<String>();//get a ascending order of records time 
			List<String> lsCallType = new ArrayList<String>();//respective type: on or off
						 
			while(!Q.isEmpty()){
				String record = Q.poll();		
				lsCallTime.add(record.substring(0,11));					
				lsCallType.add(record.substring(11));					
			}
			
			//test
//			System.out.println(lsCallType);
			
			i = 0;
			
			double [] totalAmount = new double[1];
			totalAmount[0] = 0;
			
			
			
			while(i < lsCallTime.size()){
				String onTime = null, offTime = null;
				while(i < lsCallTime.size() && lsCallType.get(i).equals("off-line")){//find first on-line
					i++;
				}				
				while(i < lsCallTime.size() && lsCallType.get(i).equals("on-line")){//find the last on-line
					onTime = lsCallTime.get(i);//t 
					i++;					
				}
				if(i < lsCallTime.size() ){//the nearest off-line as offTime
					offTime = lsCallTime.get(i);
				}
				
				if(onTime != null && offTime != null){
					onTime = onTime.substring(3);
					offTime = offTime.substring(3);
					
					res.append(onTime+" "+offTime+" "+calculate(p, onTime, offTime, totalAmount)+"\n");
					
					
				}
				
				i++;
			}
			if (totalAmount[0] > 0){
				res.deleteCharAt(res.length()-1);
				System.out.println(res.toString());
				
				String strTotalAmount = String.format("$%.2f", totalAmount[0]);
				System.out.println("Total amount: "+strTotalAmount);
			}
			
		}
		
		 
		
		
		
		
	}
	
	public static String calculate(int [] p, String onTime, String offTime, double [] totalAmount){
		double res = 0.0;
		int minCount = 0;
		
		int d1 = Integer.parseInt(onTime.substring(0,2));		//day
		int d2 = Integer.parseInt(offTime.substring(0,2));		
		int h1 = Integer.parseInt(onTime.substring(3,5));		//hour
		int h2 = Integer.parseInt(offTime.substring(3,5));
		int m1 = Integer.parseInt(onTime.substring(6,8));		//min
		int m2 = Integer.parseInt(offTime.substring(6,8));
		
		int dayDiff = d2 - d1;
		int hourDiff = h2 - h1;
		int minDiff = m2 - m1;
		
		double oneDayPrice = 0.0;
		int i;
		for(i = 0; i < 24; i++){
			oneDayPrice += p[i] * 60;
		}
		
		if(dayDiff == 0){
			if(hourDiff == 0){
				res += minDiff * p[h1];
				minCount += minDiff;
			}else{	// h2 > h1
				res += (60 - m1) * p[h1];//mins left in h1
				minCount += (60 - m1);
				
				h1++;
				while(h1 < h2){
					res += 60 * p[h1];
					minCount += 60;
					
					h1++;
				}
				res += m2 * p[h2];	// minDiff may be negative ,just to remove overadded
				minCount += m2;
			}
		}else{
			res +=  (60 - m1) * p[h1];
			minCount += (60 - m1);

			for(i = h1+1; i < 24; i++){//hours left in d1
				res += 60 * p[i];
				minCount += 60;
			}
			
			d1++;
			
			while(d1 < d2){
				res += oneDayPrice;
				minCount += 24*60;
				
				d1++;
			}
			
			for(i = 0; i < h2; i++){//hours on d2 before h2
				res += 60 * p[i];
				minCount += 60;
			}
			
			res += m2 * p[h2];
			minCount += m2;
	
		}
		
		res = res/100;
		totalAmount[0] += res;//total amount for each customer
		
		String strAmount = String.format("$%.2f", res);	
		return minCount+" "+strAmount;
	}
	
	
}


转载于:https://my.oschina.net/kaneiqi/blog/289758

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值