华为机考真题 -- 手机App防沉迷系统

题目描述:

智能手机方便了我们生活的同时,也侵占了我们不少的时间。“手机App防沉迷系统”能够让我们每天合理地规划手机App使用时间,在正确的时间做正确的事。

它的大概原理是这样的:

1. 在一天24小时内,可以注册每个App的允许使用时段;

2. 一个时间段只能使用一个App;
3 .App有优先级,数值越高,优先级越高。注册使用时段时,如果高优先级的App时间和低优先级的时段有冲突,则系统会自动注销低优先级的时段,如果App的优先级相同,则后添加的App不能注册。

请编程实现,根据输入数据注册App,并根据输入的时间点,返回时间点使用的App名称,如果该时间点没有注册任何App,请返回字符串“NA”。

输入描述:

第一行表示注册的App数量 N(N ≤ 100);
第二部分包括 N 行,每行表示一条App注册数据;
最后一行输入一个时间点,程序即返回该时间点使用的App;
例如:
2
App1 1 09:00 10:00
App2 2 11:00 11:30
09:30

数据说明如下:
1. N行注册数据以空格分隔,四项数依次表示:App名称、优先级、起始时间、结束时间
2. 优先级1~5,数字越大,优先级越高
3. 时间格式 HH:MM,小时和分钟都是两位,不足两位前面补0
4. 起始时间需小于结束时间,否则注册不上
5. 注册信息中的时间段包含起始时间点,不包含结束时间点

输出描述:

输出一个字符串,表示App名称,或NA表示空闲时间

示例1:

输入
1
App1 1 09:00 10:00
09:30

输出
App1

说明:App1注册在9点到10点间,9点半可用的应用名是App1

示例2:

输入
2
App1 1 09:00 10:00
App2 2 09:10 09:30
09:20

输出
App2

说明:App1和App2的时间段有冲突,App2的优先级比App1高,注册App2后,系统将App1的注册信息自动注销后,09:20时刻可用应用名是App2.

示例3:

输入
2
App1 1 09:00 10:00
App2 2 09:10 09:30
09:50

输出
NA

说明:App1被注销后,09:50时刻没有应用注册,因此输出NA

C++源码:

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <sstream>

using namespace std;

struct App {
    string appName;
    int priority;
    int startTime;
    int endTime;
};

// 函数声明
int timeToMinutes(const string& time);
void registerApps(int N, vector<App>& rg);
bool hasConflict(const App& newApp, const vector<App>& rg);
vector<App> removeConflictsAndAddNew(const App& newApp, vector<App>& currentApps);
void queryAppAtTime(const vector<App>& rg, const string& queryTime);

int main() {
    int N;
    cin >> N;
    cin.ignore();

    vector<App> rg;
    registerApps(N, rg);

    string queryTime;
    cin >> queryTime;
    queryAppAtTime(rg, queryTime);

    system("pause");
    return 0;
}

// 时间转换
int timeToMinutes(const string& time) {
    int hours = stoi(time.substr(0, 2));  
    int minutes = stoi(time.substr(3, 2)); 
    int allM = hours * 60 + minutes;
    return  allM;
}

// 注册应用程序并处理冲突
void registerApps(int N, vector<App>& rg) {
    int i = 0;
    while( i < N ) {
        string line;
        getline(cin, line);
        stringstream ss(line);
        string name, start, end;
        int priority;
        ss >> name >> priority >> start >> end;
        int startTime = timeToMinutes(start);
        int endTime = timeToMinutes(end) - 1;
        if (startTime > endTime) {
            i++;
            continue;
        } 

        if (hasConflict({ name, priority, startTime, endTime }, rg)) { 
            i++;
            continue;
        }

        rg = removeConflictsAndAddNew({ name, priority, startTime, endTime }, rg);
        
        i++;
    }
}

// 检查新应用是否与已注册应用冲突
bool hasConflict(const App& newApp, const vector<App>& rg) {
    for (const auto& app : rg) {
        if (newApp.startTime <= app.endTime && newApp.endTime >= app.startTime) {
            if (newApp.priority <= app.priority) {
                return true;
            }               
        }          
    }
    return false;
}

// 移除与新应用冲突的旧应用并添加新应用
vector<App> removeConflictsAndAddNew(const App& newApp, vector<App>& currentApps) {
    vector<App> res;
    for (const auto& x : currentApps) {
        if (!(newApp.startTime <= x.endTime && newApp.endTime >= x.startTime))
            res.push_back(x);
    }
    res.push_back(newApp);
    return res;
}

// 查询特定时间点的App
void queryAppAtTime(const vector<App>& rg, const string& queryTime) {
    int queryMinute = timeToMinutes(queryTime);
    for (const auto& x : rg) {
        if (x.startTime <= queryMinute && x.endTime >= queryMinute) {
            cout << x.appName << endl;
            return;
        }
    }
    cout << "NA" << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值