1016. Phone Bills (25)

A long-distance telephone company charges its customers by the following rules:

Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.

Input Specification:

Each input file contains one test case. Each case has two parts: the rate structure, and the phone call records.

The rate structure consists of a line with 24 non-negative integers denoting the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and so on for each hour in the day.

The next line contains a positive number N (<= 1000), followed by N lines of records. Each phone call record consists of the name of the customer (string of up to 20 characters without space), the time and date (mm:dd:hh:mm), and the word "on-line" or "off-line".

For each test case, all dates will be within a single month. Each "on-line" record is paired with the chronologically next record for the same customer provided it is an "off-line" record. Any "on-line" records that are not paired with an "off-line" record are ignored, as are "off-line" records not paired with an "on-line" record. It is guaranteed that at least one call is well paired in the input. You may assume that no two records for the same customer have the same time. Times are recorded using a 24-hour clock.

Output Specification:

For each test case, you must print a phone bill for each customer.

Bills must be printed in alphabetical order of customers' names. For each customer, first print in a line the name of the customer and the month of the bill in the format shown by the sample. Then for each time period of a call, print in one line the beginning and ending time and date (dd:hh:mm), the lasting time (in minute) and the charge of the call. The calls must be listed in chronological order. Finally, print the total charge for the month in the format shown by the sample.

Sample Input:
10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
10
CYLL 01:01:06:01 on-line
CYLL 01:28:16:05 off-line
CYJJ 01:01:07:00 off-line
CYLL 01:01:08:03 off-line
CYJJ 01:01:05:59 on-line
aaa 01:01:01:03 on-line
aaa 01:02:00:01 on-line
CYLL 01:28:15:41 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line
Sample Output:

CYJJ 0101:05:59 01:07:00 61 $12.10Total amount: $12.10CYLL 0101:06:01 01:08:03 122 $24.4028:15:41 28:16:05 24 $3.85Total amount: $28.25aaa 0102:00:01 04:23:59 4318 $638.80Total amount: $638.80

#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
map<string,int> ma;
int cost[24];
int all_day=0;
struct record{
	string name;
	int state;
	int time;
	record* Next;
};
vector<string> ve;
struct time_record;
struct person_record{
	time_record* Next;
	time_record* now;
	int sum;
};
struct time_record{
	int time_begin;
	int time_end;
	int state;
	time_record* Next;
};
bool compare1(record &a,record &b){
	return a.time<b.time;
}
void insert1(int time,int state,int i,person_record *person){
	if(person[i].Next==NULL){
		if(state==2)
		return;
		person[i].Next=new time_record;
		person[i].now=person[i].Next;
		person[i].now->state=1;
		person[i].now->Next=NULL;
		person[i].now->time_begin=time;
		person[i].now->time_end=-1;
		return;
	}
	if(state==1){
		if(person[i].now->state==1){
			person[i].now->time_begin=time;
			return;
		}
		
		person[i].now->Next=new time_record;
		person[i].now=person[i].now->Next;
		person[i].now->state=1;
		person[i].now->Next=NULL;
		person[i].now->time_begin=time;
		person[i].now->time_end=-1;
		return;
	}
	else{
		if(person[i].now->state==1){
			person[i].now->time_end=time;
			person[i].now->state=2;
			return;
		}
		return;
	}
}
void disp_time(int t){
	printf("%02d:%02d:%02d",t/(24*60),t%(24*60)/60,t%60);
}
double caclulate_money(int begin,int end){
	double sum=-all_day*0.6;
	int i;
	for(i=begin/(24*60);i<end/(24*60);i++)
	sum+=all_day*0.6;
	for(i=0;i<=end%(24*60)/60;i++)
	sum+=cost[i]*0.6;
	sum-=cost[i-1]*0.01*(60-end%60);
	
	for(i=23;i>=begin%(24*60)/60;i--)
	sum+=cost[i]*0.6;
	sum-=cost[i+1]*0.01*(begin%60);
	printf("%.2f",sum);
	return sum;
}
int main(){
	int N;
	int i;
		int count=0;
	int mo,d,h,m;
	for(i=0;i<24;i++){
	cin>>cost[i];
	all_day+=cost[i];
}
	cin>>N;
	string temp;
	string s;
	record *r=new record[N];
	person_record *person=new person_record[N];
		for(i=0;i<N;i++){
			person[i].Next=NULL;
			person[i].now=NULL;
			cin>>r[i].name;
			scanf("%d:%d:%d:%d",&mo,&d,&h,&m);
			r[i].time=d*24*60+h*60+m;
			cin>>s;
			if(s[1]=='n')
			r[i].state=1;
			else
			r[i].state=2;
		}
	sort(r,r+N,compare1);
		for(i=0;i<N;i++){
		//cout<<r[i].name<<" "<<r[i].time<<" "<<r[i].state<<endl;
		}
	map<string ,int>::iterator it;
	for(i=0;i<N;i++){
		it=ma.find(r[i].name);
		if(it!=ma.end()){
			insert1(r[i].time,r[i].state,it->second,person);
			//cout<<"insert:"<<it->second<<" "<<it->first<<" ";disp_time(r[i].time);cout<<endl;
		}
		else{
			ma.insert(pair<string,int>(r[i].name,count++));
			insert1(r[i].time,r[i].state,count-1,person);
			//cout<<"insert:"<<count-1<<" "<<r[i].name<<" ";disp_time(r[i].time);cout<<endl;
		}
	}
	time_record *tt;
	for(it=ma.begin();it!=ma.end();it++){
		ve.push_back(it->first);
	}
	sort(ve.begin(),ve.end());
	for(i=0;i<ve.size();i++){
		it=ma.find(ve[i]);
		tt=person[it->second].Next;
		if(tt!=NULL){
			if(tt->state!=1){
		double sum=0;
		cout<<it->first;
		printf(" %02d\n",mo);
		while(tt!=NULL){
			if(tt->state==2){
			disp_time(tt->time_begin);cout<<" ";disp_time(tt->time_end);printf(" %d $",tt->time_end-tt->time_begin);sum+=caclulate_money(tt->time_begin,tt->time_end);cout<<endl;}
			tt=tt->Next;
		} 
		printf("Total amount: $%.2f\n",sum); 
	}
	} 
}
} 

感想:

这道题烦的一比,做了我两个小时,要是没有STL估计得弄个一辈子才能弄出来。。。。

数据结构:先用map映射,把每个出现的客户都映射到一个链表头部,这个链表头部还有一个指向操作单元的指针,链表后每个单元是每次通话的时间

流程:

1.先对记录按时间排序

2.建立如上数据结构

3.根据题目要求依次插入记录

4.把map的数据转存到vector中并安string类排序规则排序

5.遍历vector和与之相连的链表

6.如果有没有话费的用户就不输出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下的Java代码实现所描述的算法: ```java import com.google.common.collect.Lists; import java.util.List; public class BillLockAlgorithm { private static final int[] BATCH_SIZES = {100, 50, 10, 1}; public void lockBills(List<Bill> billList, int syncSettlementLockBillSize) { List<List<Bill>> batchBillList = Lists.partition(billList, syncSettlementLockBillSize); for (List<Bill> bills : batchBillList) { try { billTagApplyService.lockOrUnlockBill(bills, req.getProjectId(), true, LockTypeEnum.LOCK_TYPE_ENUM_16.getCode(), propertyUser, true); } catch (Exception e) { // 锁定失败尝试分批补推 retryLockingBills(bills); } } } private void retryLockingBills(List<Bill> bills) { for (int batchSize : BATCH_SIZES) { List<List<Bill>> batchBillList = Lists.partition(bills, batchSize); for (List<Bill> batch : batchBillList) { try { billTagApplyService.lockOrUnlockBill(batch, req.getProjectId(), true, LockTypeEnum.LOCK_TYPE_ENUM_16.getCode(), propertyUser, true); } catch (Exception e) { // 锁定失败记录失败的账单 recordFailedBills(batch); } } } } private void recordFailedBills(List<Bill> bills) { // 记录失败的账单逻辑 } } ``` 你可以在 `lockBills` 方法中调用 `billTagApplyService.lockOrUnlockBill()` 方法来锁定账单。如果锁定失败,会调用 `retryLockingBills` 方法进行分批补推。在 `retryLockingBills` 方法中,会尝试使用不同的批量大小来再次锁定账单,如果仍然失败,则记录失败的账单。 请注意,以上代码是基于你提供的信息进行假设的,确保在使用之前根据你的实际情况进行适当的调整和错误处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值