PAT (Advanced Level) 1016 Phone Bills

题解

模拟。注意美元和美分的转换。如果一个人没有完整的消费记录什么都别输出 (呕~~) :)。

代码

#include<bits/stdc++.h>
using namespace std;
struct node
{
    string time,flag,other_time;
    int cost_sec,cost_money;
    node(string b,string c)
        {time=b;flag=c;}
};
struct info
{
    string name,time,flag;
    info(string a,string b,string c)
        {name=a;time=b;flag=c;}
    bool operator < (const info &n) const
        {return time<n.time;}
};
void modify(string start_time,string end_time,int &sec,int &money);
int change_time(string s);
int cal_money(int d,int h,int m);
int w_time[30];
map<string,vector<node> > smapv;
vector<info> info_v;
int main()
{
    int i,n;
    string name,time,sta;
    for(i=0;i<24;i++)
    {
        scanf("%d",&w_time[i]);
        w_time[24]+=w_time[i];
    }
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        cin>>name>>time>>sta;
        info_v.push_back(info(name,time,sta));
    }
    sort(info_v.begin(),info_v.end());
    for(i=0;i<n;i++)
    {
        name=info_v[i].name;
        time=info_v[i].time;
        sta=info_v[i].flag;
        if(smapv.find(name)!=smapv.end())
        {
            if(smapv[name].back().flag!=sta)
            {
                if(sta=="on-line")
                    smapv[name].push_back(node(time,sta));
                else
                {
                    modify(smapv[name].back().time,time,                           smapv[name].back().cost_sec,                           smapv[name].back().cost_money);
                    smapv[name].back().flag="off-line";
                    smapv[name].back().other_time=time;
                }
            }
            else
            {
                if(sta=="on-line")
                    smapv[name][smapv[name].size()-1]=(node(time,sta));
            }
        }
        else
        {
            if(sta=="on-line")
                smapv[name].push_back(node(time,sta));
        }
    }
    map<string,vector<node> > :: iterator it;
    for(it=smapv.begin();it!=smapv.end();it++)
    {
        vector<node> ans=it->second;
        int sum=0;
        if(ans[0].flag=="off-line")
            printf("%s %s\n",it->first.c_str(),ans[0].time.substr(0,2).c_str());
        for(i=0;i<ans.size();i++)
        {
            if(ans[i].flag=="off-line")
            {
                printf("%s %s %d $%.2lf\n",ans[i].time.substr(3,string::npos).c_str(),
                                           ans[i].other_time.substr(3,string::npos).c_str(),
                                           ans[i].cost_sec,
                                           ans[i].cost_money/100.0);
                sum+=ans[i].cost_money;
            }
        }
        if(sum!=0)
            printf("Total amount: $%.2lf\n",sum/100.0);
    }
    system("pause");
    return 0;
}
int change_time(string s)
{
    return (s[0]-‘0‘)*10+s[1]-‘0‘;
}
int cal_money(int d,int h,int m)
{
    int sum;
    sum=(d-1)*w_time[24]*60;
    for(int i=0;i<h;i++) sum+=w_time[i]*60;
    sum+=m*w_time[h];
    return sum;
}
void modify(string start_time,string end_time,int &sec,int &money)
{
    int i,h1,m1,h2,m2,d1,d2;
    d1=change_time(start_time.substr(3,2));
    h1=change_time(start_time.substr(6,2));
    m1=change_time(start_time.substr(9,2));
    d2=change_time(end_time.substr(3,2));
    h2=change_time(end_time.substr(6,2));
    m2=change_time(end_time.substr(9,2));
    sec=d2*1440+h2*60+m2-d1*1440-h1*60-m1;
    money=cal_money(d2,h2,m2)-cal_money(d1,h1,m1);
}
/*
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
*/

原文地址:https://www.cnblogs.com/VividBinGo/p/12219859.html

时间: 2024-10-19 08:03:09

PAT (Advanced Level) 1016 Phone Bills的相关文章

PAT (Advanced Level) 1016. Phone Bills (25)

简单模拟题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> using namespace std; int cost[30]; int n; struct X { string name; int dd,hh,mm,len,f;

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: 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 lon

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c