PAT刷题-排序:1016 Phone Bills

本文分享了一个使用C++实现的计费系统代码,详细展示了如何处理用户在线和离线状态,计算费用,并进行排序和输出。代码中包含了日期时间转换、费用计算逻辑和用户数据结构的设计。

过一个测试点就15分了......

搞了一上午,搞出个15分,本以为一分也拿不到

思路跟《算法笔记》完全不一样,今天这道题就到这儿,明天再修改,花时间有点多了

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

typedef struct
{
    bool on_off_line;//1-在线,0-离线
    int time, month,date,hour,min;
    char name[21];
} Bill;

int rate[24];
Bill bill[1024];

bool cmp(Bill x, Bill y)
{
    if (strcmp(x.name, y.name) != 0)
        return strcmp(x.name, y.name) < 0;
    else
        return x.time < y.time;
}

int main()
{
    int i, n, time, j;
    double tax, total_tax;
    char name[21];
    string temp;

    for (i = 0; i < 24; i++)
        scanf("%d", &rate[i]);
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%s", &bill[i].name);
        scanf("%d:%d:%d:%d", &bill[i].month, &bill[i].date, &bill[i].hour, &bill[i].min);
        //将  (月:日:时:分)  转换为分钟
        bill[i].time = bill[i].date * 24 * 60 + bill[i].hour * 60 + bill[i].min;
        getline(cin, temp);
        if(temp[2]=='n')
            bill[i].on_off_line = 1;
        else
            bill[i].on_off_line = 0;
    }

    sort(bill, bill + n, cmp);

    tax = total_tax = 0;
    printf("%s %02d\n", bill[0].name, bill[0].date);
    for (i = 0; i < n; i++)
    {
        if (strcmp(bill[i].name, bill[i + 1].name) != 0)
        {
            printf("Total amount: $%.2lf\n", total_tax / 100);
            if (i + 1 < n)
                printf("%s %02d\n", bill[i + 1].name, bill[i + 1].month);
            total_tax = 0;
        }
        else
        {
            if (bill[i].on_off_line && !bill[i+1].on_off_line)
            {
                if (bill[i].date != bill[i + 1].date)
                {
                    tax = (bill[i + 1].date - bill[i].date) * 355 * 60;
                    tax += bill[i + 1].min * rate[bill[i + 1].hour];
                    for (j = 0; j < bill[i + 1].hour; j++)
                        tax += rate[j] * 60;
                    tax -= bill[i].min * rate[bill[i].hour];
                    for (j = 0; j < bill[i].hour;j++)
                        tax -= rate[j] * 60;
                    time = bill[i+1].time - bill[i].time;
                }
                else
                {
                    time = bill[i+1].time - bill[i].time;
                    tax = (60 - bill[i].min) * rate[bill[i].hour];
                    tax += bill[i + 1].min * rate[bill[i + 1].hour];
                    tax += (bill[i + 1].hour - bill[i].hour - 1) * rate[bill[i].hour + 1] * 60;
                }
                printf("%02d:%02d:%02d ", bill[i].date, bill[i].hour, bill[i].min);
                printf("%02d:%02d:%02d ", bill[i + 1].date, bill[i + 1].hour, bill[i + 1].min);
                printf("%d ", time);
                printf("$%.2lf\n", tax / 100);
                total_tax += tax;
            }
        }
    }
    getchar();
}

/*
CYJJ 1799 1
CYJJ 1860 0
CYLL 1801 1
CYLL 1923 0
CYLL 41261 1
CYLL 41285 0
aaa 1503 1
aaa 2881 1
aaa 7199 0
aaa 7344 1
*/

 

整体帮我设计一下, <!DOCTYPE html> <html lang="zh-Hant"> <head> <meta charset="UTF-8" /> <title>直播打賞後台</title> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f9; margin: 0; padding: 0; } .navbar { background-color: #007bff; color: #fff; padding: 10px 20px; display: flex; justify-content: space-between; align-items: center; } .navbar a { color: #fff; text-decoration: none; margin: 0 10px; font-size: 16px; } .navbar a:hover { text-decoration: underline; } .container { padding: 20px; } .table-container { margin-bottom: 20px; } table { width: 100%; border-collapse: collapse; margin-top: 10px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #007bff; color: #fff; } tr:nth-child(even) { background-color: #f2f2f2; } tr:hover { background-color: #ddd; } .form-container { margin-bottom: 20px; padding: 10px; border: 1px solid #ccc; border-radius: 4px; background-color: #fff; } .form-container h3 { margin-top: 0; } .form-container input[type="text"], .form-container input[type="number"] { width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } .form-container button { width: 100%; padding: 10px; background-color: #007bff; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; } .form-container button:hover { background-color: #0056b3; } .modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0, 0, 0, 0.4); } .modal-content { background-color: #fefefe; margin: 15% auto; padding: 20px; border: 1px solid #888; width: 300px; border-radius: 4px; } .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; } </style> </head> <body> <div class="navbar"> <div> <a href="/dashboard">充提记录</a> <a href="/members">会员列表</a> <a href="/history">历史账单</a> </div> <div> <a href="/logout">退出登录</a> </div> </div> <div class="container"> <h2>会员列表</h2> <div class="table-container"> <table> <tr> <th>用戶ID</th> <th>用戶名</th> <th>余额</th> <th>操作</th> </tr> {% for member in members %} <tr> <td>{{ member.user_id }}</td> <td>{{ member.name }}</td> <td>{{ member.balance }}</td> <td> <button onclick="openModal({{ member.user_id }})"> 充值/提现 </button> </td> </tr> {% endfor %} </table> </div> <div id="myModal" class="modal"> <div class="modal-content"> <span class="close" onclick="closeModal()">×</span> <h2>充值/提现</h2> <form action="/recharge" method="post"> <input name="user_id" id="modal_user_id" type="hidden" /> <input name="amount" type="number" placeholder="金额" required /><br /> <button type="submit">确认</button> </form> </div> </div> <h2>充提记录</h2> <div class="table-container"> <table> <tr> <th>用戶ID</th> <th>金額</th> <th>類型</th> <th>時間</th> </tr> {% for record in records %} <tr> <td>{{ record.user_id }}</td> <td>{{ record.amount }}</td> <td>{{ record.type }}</td> <td>{{ record.created_at }}</td> </tr> {% endfor %} </table> </div> <h2>历史账单</h2> <div class="table-container"> <table> <tr> <th>用戶ID</th> <th>金額</th> <th>時間</th> </tr> {% for bill in bills %} <tr> <td>{{ bill.user_id }}</td> <td>{{ bill.amount }}</td> <td>{{ bill.created_at }}</td> </tr> {% endfor %} </table> </div> </div> <script> function openModal(user_id) { document.getElementById("modal_user_id").value = user_id; document.getElementById("myModal").style.display = "block"; } function closeModal() { document.getElementById("myModal").style.display = "none"; } window.onclick = function (event) { if (event.target == document.getElementById("myModal")) { closeModal(); } }; </script> </body> </html>
07-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值