1016 Phone Bills 测试点1、2
易错点
测试点1、2:注意没有匹配成功的对的时候,结果不输出
如可尝试下列数据:
10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
2
CYLL 01:01:06:01 on-line
aaa 01:04:23:59 off-line
代码
#include <iostream>
#include <cstdio>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <iomanip>
using namespace std;
int price[25];//每小时的话费价格 cents/min
struct pair3{
string first;
string second;
string third;
};
bool cmp_diy(pair3& a,pair3& b){
if((a.second).substr(3,2)!=(b.second).substr(3,2)){
return stoi((a.second).substr(3,2))<stoi((b.second).substr(3,2));
}
else{
if((a.second).substr(6,2)!=(b.second).substr(6,2)){
return stoi((a.second).substr(6,2))<stoi((b.second).substr(6,2));
}
else{
return stoi((a.second).substr(9,2))<stoi((b.second).substr(9,2));
}
}
}
bool cmp_diy2(vector<pair3> a,vector<pair3> b){//将所有数据按名字的字母序排序
return a[0].first<b[0].first;
}
int cal_time(string a,string b){//a>b 返回值的单位:min
int t1,t2,time;
t1=(((a[0]-'0')*10+(a[1]-'0'))*24*60+((a[3]-'0')*10+(a[4]-'0'))*60+(a[6]-'0')*10+(a[7]-'0'));
t2=(((b[0]-'0')*10+(b[1]-'0'))*24*60+((b[3]-'0')*10+(b[4]-'0'))*60+(b[6]-'0')*10+(b[7]-'0'));
time=t1-t2;
return time;
}
float cal_price(string a,string b){//a>b a型如 dd:hh:mm
string h1,h2;
float cnt=0;
h1=a.substr(0,5);
h2=b.substr(0,5);
if(h1==h2){//同一天同一小时内
cnt=0.01*cal_time(a,b)*price[stoi(a.substr(3,2))];
return cnt;
}
else{
for(int i=stoi(b.substr(3,2))+1;i<((stoi(a.substr(0,2))-stoi(b.substr(0,2)))*24+stoi(a.substr(3,2)));i++){
cnt+=(0.6*price[i%24]);
}
cnt+=(0.01*(60-stoi(b.substr(6,2)))*price[stoi(b.substr(3,2))]);
cnt+=(0.01*(stoi(a.substr(6,2)))*price[stoi(a.substr(3,2))]);
return cnt;
}
}
int main()
{
int n;
string name,time,state;
pair3 pa;
set<string> st;//利用去重和查找功能,来给数据分类
vector<pair3> vt1;//vt1用来存放不同的数据分类
vector<vector<pair3> > vt2;//vt2用来存放vt1
string tmp;//用来记录指定名字和月份的数据是否有重复出现过
map<string,int> mp;//tmp(名字+日期,组成的字符串),对应存放该类数据的分类在vt2中的序号
int cnt=0;//该变量用于记录不同tmp(名字+日期,组成的字符串)对应的存放该类数据的分类在vt2中的序号
int tmp2;
int tmp3;//记录有没有匹配成功
float cnt2;//用于记录累计话费;
for(int i=0;i<24;i++){
cin>>price[i];
}
cin>>n;
for(int i=0;i<n;i++){
cin>>name>>time>>state;
pa.first=name;
pa.second=time;
pa.third=state;
tmp=name+time.substr(0,2);
if(st.find(tmp)!=st.end()){//如果已经有这个分类,就把数据放到对应的分类里
vt2[mp[tmp]].push_back(pa);
}
else{//输入的数据的"用户名字+月份"还未出现过
st.insert(tmp);
vt1.push_back(pa);
vt2.push_back(vt1);
mp[tmp]=cnt;
cnt++;
vt1.pop_back();
}
}
//输入的数据有三部分:名字;时间;接/挂
//将每条输入数据,按用户名和月份分类
//将分好的类,再按时间升序排序
//排好序后遍历,如果是on,用临时变量记录这一条的数据,先看下一条
//下一条是off则配对成功
//下一条如果是on,则上一条的数据不要,临时变量记录这一条的数据,继续向下遍历
//配对成功,计算价格
sort(vt2.begin(),vt2.begin()+vt2.size(),cmp_diy2);
for(int i=0;i<vt2.size();i++){
sort(vt2[i].begin(),vt2[i].begin()+(vt2[i]).size(),cmp_diy);
tmp2=0;
tmp3=0;
cnt2=0;
for(int j=0;j<(vt2[i]).size();j++){
if(tmp2==0){
if((vt2[i])[j].third=="on-line"){
tmp2=1;
}
else if((vt2[i])[j].third=="off-line"){//太早出现的off-line无法匹配,丢弃
(vt2[i]).erase((vt2[i]).begin()+j);
j--;
}
}
else{
if((vt2[i])[j].third=="on-line"){//丢弃上一个on-line的数据
(vt2[i]).erase((vt2[i]).begin()+j-1);
j--;
}
else if((vt2[i])[j].third=="off-line"){//配对成功
if(tmp3==0){
tmp3=1;
cout<<(vt2[i])[0].first<<" "<<((vt2[i])[0].second).substr(0,2)<<endl;
}
cout<<((vt2[i])[j-1].second).substr(3,8)<<" "<<((vt2[i])[j].second).substr(3,8)<<" ";
cout<<cal_time(((vt2[i])[j].second).substr(3,8),((vt2[i])[j-1].second).substr(3,8))<<" $";
cout<<fixed<<setprecision(2)<<cal_price(((vt2[i])[j].second).substr(3,8),((vt2[i])[j-1].second).substr(3,8));
cout<<endl;
tmp2=0;
cnt2+=cal_price(((vt2[i])[j].second).substr(3,8),((vt2[i])[j-1].second).substr(3,8));
}
}
}
if(tmp3==1){
cout<<"Total amount: $";
cout<<fixed<<setprecision(2)<<cnt2;
cout<<endl;
}
//筛选完剩下的都是能两两匹配的数据
}
return 0;
}

浙公网安备 33010602011771号