PAT 1016. Phone Bills

题目:http://pat.zju.edu.cn/contests/pat-a-practise/1016

题解:

模拟题。

给一份流水账,求各个用户改缴多少费用。

注意:只有on-off匹配才需缴费

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0x6fffffff
struct record
{
    char name[25];
    int mon;
    int day;
    int hour;
    int minute;
    char line[15];
}rec[1005];
bool cmp(const struct record &a,const struct record &b)//对输入排序
{
    int temp=strcmp(a.name,b.name);
    if(temp==0)//相同名字按时间排序
    {
        if(a.mon!=b.mon)
            return a.mon<b.mon;
        if(a.day!=b.day)
            return a.day<b.day;
        if(a.hour!=b.hour)
            return a.hour<b.hour;
        if(a.minute!=b.minute)
            return a.minute<b.minute;
    }
    else
        return temp<0;
}
int check(int st,int n)//确定on后面有对应off
{
    char name[25];
    bool flag=false,out=false;
    strcpy(name,rec[st].name);
    for(int i=st;i<n;++i)
    {
        if(strcmp(rec[i].name,name)==0)
        {
            if(rec[i].line[1]=='n')
                flag=true;
            else if(rec[i].line[1]=='f'&&flag)
            {
                flag=false;
                out=true;
            }
        }
        else
            break;
    }
    return out;
}
void checkMoney(struct record st,struct record en,int* rate,int &time,double &money)//计算费用
{
    struct record r;
    r.day=st.day;
    r.hour=st.hour;
    r.minute=st.minute;
    time=0;
    money=0;
    for(;r.day<en.day||r.hour<en.hour||r.minute<en.minute;)
    {
        money+=rate[r.hour];
        ++time;
        ++r.minute;
        if(r.minute>=60)
        {
            r.minute=0;
            ++r.hour;
            if(r.hour>=24)
            {
                r.hour=0;
                ++r.day;
            }
        }
    }
    money/=100;
}
int main()
{
    int n;
    int rate[25];
    int time;
    double money,moneyAll;
    char name[25];
    bool flag;
    struct record r;
    for(int i=0;i<24;++i)
        scanf("%d",rate+i);
    scanf("%d",&n);
    for(int i=0;i<n;++i)
    {
        scanf("%s",rec[i].name);
        scanf("%d:%d:%d:%d",&rec[i].mon,&rec[i].day,&rec[i].hour,&rec[i].minute);
        scanf("%s",rec[i].line);
    }
    sort(rec,rec+n,cmp);
    for(int i=0,out;i<n;)
    {
        out=check(i,n);
        if(out)
        {
            flag=false;
            moneyAll=0;
            printf("%s %02d\n",rec[i].name,rec[i].mon);
            strcpy(name,rec[i].name);
            for(;strcmp(name,rec[i].name)==0;++i)
            {
                if(rec[i].line[1]=='n')
                {
                    r.day=rec[i].day;
                    r.hour=rec[i].hour;
                    r.minute=rec[i].minute;
                    flag=true;
                }
                else if(rec[i].line[1]=='f'&&flag)
                {
                    flag=false;
                    printf("%02d:%02d:%02d %02d:%02d:%02d",r.day,r.hour,r.minute,rec[i].day,rec[i].hour,rec[i].minute);
                    checkMoney(r,rec[i],rate,time,money);
                    printf(" %d $%.2f\n",time,money);
                    moneyAll+=money;
                }
            }
            printf("Total amount: $%.2f\n",moneyAll);
        }
        else
            ++i;
    }
    return 0;
}

来源: http://blog.csdn.net/acm_ted/article/details/20483257

你可能感兴趣的:(PAT 1016. Phone Bills)