PAT甲级-排序类型-1016 Phone Bills解题思路

1016 Phone Bills (25 分)

在这里插入图片描述
在这里插入图片描述

思路

这道题代码量可不少了,虽然没用到什么算法。
计算时间差,将两个时间戳转换成多少秒,再做差;
计算花费,写了个循环,计算钱,比那种一秒一秒算钱的快不少。

strcmp strcpy这些都要用的熟练才行~

最难的是比较前后是否匹配了,还有if-else真的别乱加,得细细斟酌,就是else多加一句导致由两个测试点不能通过,debug了半天,PAT测试点2、3都是大量数据,大概50条以上这样子。可以通过牛客网的题目测试点帮助检查。
牛客网链接🔗

代码

#include <bits/stdc++.h>
using namespace std;

struct stu
{
    char id[25];
    int month,dd,hh,mm;
    bool status;
}stud[2005];

bool com(stu a,stu b)
{
    int s =strcmp(a.id,b.id);
    if(s != 0)
        return s<0;
    else if(a.month != b.month)return a.month<b.month;
    else if(a.dd!=b.dd)return a.dd < b.dd;
    else if(a.hh!=b.hh)return a.hh <b.hh;
    else return a.mm<b.mm;
}
int num[25];

int compute_time(int d1,int d2,int h1,int h2,int  m1,int m2)
{
    int t1 = 0 ,t2 = 0;
    if(d1 == d2)
    {
        t1  = h1*60 +m1 ;
        t2 = h2*60 +m2 ;
        return t1 - t2;
    }
    else
    {
        int ttt = 24 * 60;
        int d = d1 -d2;
        t1  = h1*60 +m1 ;
        t2 = h2*60 +m2 ;

        int all_te = (d-1)*ttt + t1 -t2 +ttt;
        return all_te;
    }
}

int compute_money(int d2,int h2,int m2,int all_time)
{
    int money = 0;
    while(all_time>=0)
    {
        if((all_time + m2)>60)
        {
            money += num[h2]*(60-m2);
            h2 = (h2+1)%24;
            all_time = all_time -(60-m2);
            m2 = 0;
        }
        else 
        {
            money += num[h2]*(all_time);
            return money;
        }
    }
    return money ;
}

int main()
{
    
    for(int i =0;i<24;i++)
        cin>>num[i];

    int N;

    cin>>N;
    char sta[20];
    for(int i = 0;i<N;i++)
    {
        scanf("%s %d:%d:%d:%d %s",stud[i].id,&stud[i].month,&stud[i].dd,&stud[i].hh,&stud[i].mm,sta);
        if(!strcmp(sta,"on-line") )
            stud[i].status = 0;
        else 
            stud[i].status = 1;
    }

    sort(stud,stud+N,com);
   

    int flag = 0;
    char start_name[25]="lukeda";
    int start_status = -1;
    double all_money = 0;
    int d2,h2,m2;
    for(int i=0;i<N;i++)
    {
        //cout<<stud[i].id<<" "<<stud[i].status<<endl;
        if(stud[i].status == 0 )
        {
            if (!strcmp(start_name , stud[i].id))
            {
                strcpy(start_name,stud[i].id);
                d2 = stud[i].dd;
                h2 = stud[i].hh;
                m2 = stud[i].mm;
                start_status = 0;
                continue;
            }
            else
            {
                strcpy(start_name,stud[i].id);
                d2 = stud[i].dd;
                h2 = stud[i].hh;
                m2 = stud[i].mm;
                if (all_money)
                    printf("Total amount: $%.2f\n",all_money);
                all_money = 0;
                start_status = 0;
                flag = 0;
                
                continue;
            }
        }
        else if(stud[i].status == 1)
        {
            if (!strcmp(start_name , stud[i].id) && start_status==0)
            {
                int all_time = compute_time(stud[i].dd ,d2 ,stud[i].hh,h2,stud[i].mm,m2);
                double money = compute_money(d2,h2,m2,all_time);
                money /=100;
                all_money += money;
                start_status = 1;
                flag += 1;
                if(flag==1)
                    printf("%s %02d\n",stud[i].id,stud[i].month);
                printf("%02d:%02d:%02d %02d:%02d:%02d",d2,h2,m2,stud[i].dd,stud[i].hh,stud[i].mm);
                printf(" %d $%.2f\n",all_time,money);
             
            }
            else
            {
                start_status = 1;
            }
        }
    }
    if (all_money)
        printf("Total amount: $%.2f\n",all_money);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wujiekd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值