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 */
上一篇:sql中某一个字段内容为用逗号分割的字符串转换成多条数据并附Id


下一篇:PAT (Advanced Level) 1057 Stack