手机App防沉迷系统 - 华为OD统一考试

news2024/9/29 23:30:32

OD统一考试(C卷)

分值: 100分

题解: Java / Python / C++

alt

题目描述

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

它的大概原理是这样的:

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

image-20240122122323329

2、一个时间段只能使用一个APP, 不能在同时注册App2 和 App3;

image-20240122122640906

3、App有优先级,数值越高,优先级越高。注册使用时段时,如果高优先级的App时间和低优先级的时段有冲突,则系统会自动注销低优先级的时段;

如果App的优先级相同,则后添加的App不能注册。

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

输入描述

第一行表示注册的App数N ( N ≤ 100 ) (N\le 100) (N100)

第二部分包括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优先级高,注册App2之后,App1自动注销,因此输出App2

示例3

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

输出:
NA

题解

这是一个模拟题,需要按照题目描述的规则模拟注册过程,并在给定时间点找到注册的App。

程序的解题思路和关键步骤:

  1. 定义一个App类,用于表示应用程序对象,包括应用程序名称、优先级、开始时间和结束时间。同时,在App类中实现一个方法判断两个应用程序对象是否有时间重叠。
  2. 编写convertToMinutes方法,将时间字符串转换为分钟表示的整数。
  3. 编写convertToApp方法,将输入的字符串转换为应用程序对象。
  4. 通过读取输入,获取应用程序的数量,以及每个应用程序的注册信息。将注册信息转换为App对象,并根据题目规则进行注册。如果新注册的应用程序与已有的应用程序有时间重叠,根据优先级判断是否需要注销低优先级的应用程序。
  5. 读取给定的时间点,将其转换为分钟表示的整数。
  6. 遍历已注册的应用程序列表,找到包含给定时间的应用程序。
  7. 输出结果。

该程序使用了类的封装和集合的操作,通过遍历和比较,实现了应用程序的注册和冲突处理。最后,根据给定时间点找到相应的应用程序并输出结果。

时间复杂度:O(N^2),其中N为应用程序的数量。在每次注册应用程序时,都需要遍历已有的应用程序列表,判断是否有时间重叠。

空间复杂度:O(N),存储了已注册的应用程序列表。

Java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class App {
    String name;        // 应用程序名称
    int priority;       // 优先级
    int startTime;      // 开始时间(以分钟为单位)
    int endTime;        // 结束时间(以分钟为单位)

    // 构造函数,用于初始化应用程序对象
    public App(String name, int priority, int startTime, int endTime) {
        this.name = name;
        this.priority = priority;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    // 判断当前应用程序与另一个应用程序是否有时间重叠
    public boolean hasOverlap(App other) {
        return this.startTime < other.endTime && other.startTime < this.endTime;
    }
}
/**
 * @author code5bug
 */
class Main {
    // 将时间字符串转换为分钟表示的整数
    public static int convertToMinutes(String time) {
        String[] arr = time.split(":");
        int hour = Integer.parseInt(arr[0]);
        int minute = Integer.parseInt(arr[1]);
        return hour * 60 + minute;
    }

    // 将输入字符串转换为应用程序对象
    public static App convertToApp(String input) {
        String[] arr = input.split(" ");
        String name = arr[0];
        int priority = Integer.parseInt(arr[1]);
        int startTime = convertToMinutes(arr[2]);
        int endTime = convertToMinutes(arr[3]);
        return new App(name, priority, startTime, endTime);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int numApps = scanner.nextInt();   // 输入应用程序的数量
        scanner.nextLine(); // 消耗掉换行符

        // 存储应用程序对象的数组
        List<App> apps = new ArrayList<>();

        // 读取并转换应用程序信息
        for (int i = 0; i < numApps; i++) {
            String input = scanner.nextLine();
            App app = convertToApp(input);

            // 如果开始时间大于结束时间,则忽略该应用程序
            if (app.startTime > app.endTime) {
                continue;
            }

            // 与当前应用程序时间重叠的应用程序
            List<App> overlappingApps = new ArrayList<>();
            int maxPriority = -1;
            for (int j = 0; j < apps.size(); j++) {
                App tmp = apps.get(j);
                if (app.hasOverlap(tmp)) {
                    overlappingApps.add(tmp);
                    maxPriority = Math.max(maxPriority, tmp.priority);
                }
            }

            // 当前App与已有的App的优先级相同,则后添加的App不能注册
            // 高优先级的App时间和低优先级的时段有冲突,则系统会自动注销低优先级的时段( 当前App优先级低于其他冲突的App 则也不能注册)
            if (app.priority <= maxPriority) {
                continue;
            } else {
                // 将之前已经注册优先级较低有时段有冲突的App 注销掉
                apps.removeAll(overlappingApps);
            }

            // 注册当前 App
            apps.add(app);
        }


        int givenTime = convertToMinutes(scanner.nextLine());  // 给定时间
        String answer = "NA";

        // 遍历结果列表,找到包含给定时间的应用程序
        for (App app : apps) {
            if (app.startTime <= givenTime && givenTime < app.endTime) {
                answer = app.name;
                break;
            }
        }

        System.out.println(answer);  // 输出结果
    }
}

Python

class App:
    def __init__(self, name, priority, start_time, end_time):
        self.name = name
        self.priority = priority
        self.start_time = start_time
        self.end_time = end_time

    def has_overlap(self, other):
        return self.start_time < other.end_time and other.start_time < self.end_time

# 将时间字符串转换为分钟表示的整数
def convert_to_minutes(time_str):
    hour, minute = map(int, time_str.split(":"))
    return hour * 60 + minute

# 将输入字符串转换为应用程序对象
def convert_to_app(input_str):
    parts = input_str.split()
    name = parts[0]
    priority = int(parts[1])
    start_time = convert_to_minutes(parts[2])
    end_time = convert_to_minutes(parts[3])
    return App(name, priority, start_time, end_time)

if __name__ == "__main__":
    num_apps = int(input())
    apps = []

    # 读取并转换应用程序信息
    for _ in range(num_apps):
        app_input = input()
        app = convert_to_app(app_input)

        # 如果开始时间大于结束时间,则忽略该应用程序
        if app.start_time > app.end_time:
            continue

        overlapping_apps = []
        max_priority = -1

        # 与当前应用程序时间重叠的应用程序
        for tmp in apps:
            if app.has_overlap(tmp):
                overlapping_apps.append(tmp)
                max_priority = max(max_priority, tmp.priority)

        # 当前App与已有的App的优先级相同,则后添加的App不能注册
        # 高优先级的App时间和低优先级的时段有冲突,则系统会自动注销低优先级的时段
        if app.priority <= max_priority:
            continue
        else:
            # 将之前已经注册优先级较低有时段有冲突的App 注销掉
            for tmp in overlapping_apps:
                apps.remove(tmp)

        # 注册当前 App
        apps.append(app)

    given_time = convert_to_minutes(input())  # 给定时间
    answer = "NA"

    # 遍历结果列表,找到包含给定时间的应用程序
    for app in apps:
        if app.start_time <= given_time < app.end_time:
            answer = app.name
            break

    print(answer)  # 输出结果

C++

#include <bits/stdc++.h>

using namespace std;

class App {
public:
    string name;        // 应用程序名称
    int priority;       // 优先级
    int startTime;      // 开始时间(以分钟为单位)
    int endTime;        // 结束时间(以分钟为单位)

    // 构造函数,用于初始化应用程序对象
    App(string name, int priority, int startTime, int endTime) {
        this->name = name;
        this->priority = priority;
        this->startTime = startTime;
        this->endTime = endTime;
    }

    // 判断当前应用程序与另一个应用程序是否有时间重叠
    bool hasOverlap(const App& other) const {
        return this->startTime < other.endTime && other.startTime < this->endTime;
    }
};

// 将时间字符串转换为分钟表示的整数
int convertToMinutes(const string& time) {
    size_t pos = time.find(":");
    int hour = stoi(time.substr(0, pos));
    int minute = stoi(time.substr(pos + 1));
    return hour * 60 + minute;
}

// 将输入字符串转换为应用程序对象
App convertToApp(const string& input) {
    size_t pos1 = input.find(" ");
    size_t pos2 = input.find(" ", pos1 + 1);
    size_t pos3 = input.find(" ", pos2 + 1);

    string name = input.substr(0, pos1);
    int priority = stoi(input.substr(pos1 + 1, pos2 - pos1 - 1));
    int startTime = convertToMinutes(input.substr(pos2 + 1, pos3 - pos2 - 1));
    int endTime = convertToMinutes(input.substr(pos3 + 1));

    return App(name, priority, startTime, endTime);
}

int main() {
    int numApps;
    cin >> numApps;   // 输入应用程序的数量
    cin.ignore(); // 消耗掉换行符

    // 存储应用程序对象的数组
    vector<App> apps;

    // 读取并转换应用程序信息
    for (int i = 0; i < numApps; i++) {
        string input;
        getline(cin, input);
        App app = convertToApp(input);

        // 如果开始时间大于结束时间,则忽略该应用程序
        if (app.startTime > app.endTime) {
            continue;
        }

        // 与当前应用程序时间重叠的应用程序
        vector<int> overlappingApps;
        int maxPriority = -1;
        for (int j = 0; j < apps.size(); j++) {
            App tmp = apps[j];
            if (app.hasOverlap(tmp)) {
                overlappingApps.push_back(j);
                maxPriority = max(maxPriority, tmp.priority);
            }
        }

        // 当前App与已有的App的优先级相同,则后添加的App不能注册
        // 高优先级的App时间和低优先级的时段有冲突,则系统会自动注销低优先级的时段( 当前App优先级低于其他冲突的App 则也不能注册)
        if (app.priority <= maxPriority) {
            continue;
        } else {
            // 将之前已经注册优先级较低有时段有冲突的App 注销掉
            for(int j = overlappingApps.size() - 1; j >= 0; j--){
                apps.erase(apps.begin() + overlappingApps[j]);
            }
        }

        // 注册当前 App
        apps.push_back(app);
    }

    int givenTime;
    string givenTimeString;
    getline(cin, givenTimeString);
    givenTime = convertToMinutes(givenTimeString);  // 给定时间
    string answer = "NA";

    // 遍历结果列表,找到包含给定时间的应用程序
    for (const App& app : apps) {
        if (app.startTime <= givenTime && givenTime < app.endTime) {
            answer = app.name;
            break;
        }
    }

    cout << answer << endl;  // 输出结果

    return 0;
}

‍❤️‍华为OD机试面试交流群每日真题分享): 加V时备注“华为od加群”

🙏整理题解不易, 如果有帮助到您,请给点个赞 ‍❤️‍ 和收藏 ⭐,让更多的人看到。🙏🙏🙏

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1407402.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Redis - redis.windows.conf配置文件及RDB和AOF数据持久化方案

Redis - redis.windows.conf配置文件及RDB和AOF数据持久化方案 Redis的高性能是由于其将所有数据都存储在了内存中&#xff0c;为了使Redis在重启之后仍能保证数据不丢失&#xff0c;需要将数据从内存中同步到硬盘中&#xff0c;这一过程就是持久化。 Redis支持两种方式的持久化…

Vue3 Suspense 优雅地处理异步组件加载

✨ 专栏介绍 在当今Web开发领域中&#xff0c;构建交互性强、可复用且易于维护的用户界面是至关重要的。而Vue.js作为一款现代化且流行的JavaScript框架&#xff0c;正是为了满足这些需求而诞生。它采用了MVVM架构模式&#xff0c;并通过数据驱动和组件化的方式&#xff0c;使…

[Linux]HTTP状态响应码和示例

1xx&#xff1a;信息响应类&#xff0c;表示接收到请求并且继续处理 2xx&#xff1a;处理成功响应类&#xff0c;表示动作被成功接收、理解和接受 3xx&#xff1a;重定向响应类&#xff0c;为了完成指定的动作&#xff0c;必须接受进一步处理 4xx&#xff1a;客户端错误&#x…

[笔记]Spring AOP

Spring AOP&#xff08;Aspect Oriented Programming&#xff09; AOP将应用程序分为核心业务和非核心的公共功能&#xff0c;AOP的关注点是系统中的非核心的公共功能&#xff1b; AOP可以通过预编译或者运行期动态代理的方式&#xff0c;为横跨多个对象&#xff08;没有继承关…

四、Flask学习之JavaScript

四、Flask学习之JavaScript JavaScript&#xff0c;作为一种前端脚本语言&#xff0c;赋予网页生动的交互性和动态性。通过它&#xff0c;开发者能够操作DOM&#xff08;文档对象模型&#xff09;实现页面元素的动态改变、响应用户事件&#xff0c;并借助AJAX技术实现异步数据…

omron adept控制器维修SmartController EX

欧姆龙机器人adept运动控制器维修SmartController EX 19300-000 维修范围&#xff1a;姆龙机器人&#xff1b;码垛机器人&#xff1b;搬运机器人&#xff1b;焊机机器人&#xff1b;变位机等。 Adept Viper s650/s850用于装配、物料搬运、包装和机械装卸&#xff0c;循环周期短…

基于YOLOv8深度学习的102种花卉智能识别系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战

《博主简介》 小伙伴们好&#xff0c;我是阿旭。专注于人工智能、AIGC、python、计算机视觉相关分享研究。 ✌更多学习资源&#xff0c;可关注公-仲-hao:【阿旭算法与机器学习】&#xff0c;共同学习交流~ &#x1f44d;感谢小伙伴们点赞、关注&#xff01; 《------往期经典推…

dom-to-image-more 使用

与网上不同的使用方式&#xff1a; 官网 dom-to-image-more - npm 这里不会出现两行缩略不行的bug yarn add dom-to-image-more 下面 生成图片并下载图片 const picture ref() const dom2img () > {var node picture.valuedomtoimage.toPng(node, { cacheBust: t…

iou的cpu和gpu源码实现

本专栏主要是深度学习/自动驾驶相关的源码实现,获取全套代码请参考 简介 IoU&#xff08;Intersection over Union&#xff09;是一种测量在特定数据集中检测相应物体准确度的一个标准&#xff0c;通常用于目标检测中预测框&#xff08;bounding box&#xff09;之间准确度的…

Arduino U8g2库:图形界面库的强大利器,

Arduino U8g2库&#xff1a;图形界面库的强大利器 介绍 在Arduino世界中&#xff0c;图形界面的显示通常是一项关键的任务。为了简化这个过程&#xff0c;提高开发效率&#xff0c;许多库被开发出来&#xff0c;其中U8g2库就是其中之一。U8g2库是一个功能强大的图形库&#x…

uniapp复选框 实现排他选项

选择了排他选项之后 复选框其他选项不可以选择 <view class"reportData" v-for"(val, index) in obj" :key"index"> <view v-if"val.type 3" ><u-checkbox-group v-model"optionValue" placement"colu…

web系统服务器监控检查

一、检查操作系统是否存在增减文件&#xff0c;是否有shell被上传 要检查操作系统是否存在增减文件或是否有shell被上传&#xff0c;您可以按照以下步骤进行操作&#xff1a; 文件完整性检查&#xff1a; 使用文件系统的完整性检查工具&#xff0c;例如fsck&#xff08;对于ext…

项目一:踏上Java开发之旅

文章目录 一、实战概述二、实战步骤任务1&#xff1a;安装配置JDK并开发第一个Java程序步骤一&#xff1a;安装JDK步骤二&#xff1a;配置JDK环境变量步骤三&#xff1a;开发第一个Java程序 课堂练习任务1、打印个人信息任务2、打印直角三角形任务3、打印一颗爱心任务4、打印史…

git:使用git rebase合并多次commit为一个

git log&#xff1a;找到需要合并的最早 commit 的父级 git rebase -i 73a5cd8597除第一个 pick 外&#xff0c;将其它改成 s&#xff0c;改完后保存退出 保存完后弹出 commit message 合并提示&#xff0c;根据这次合并的目的&#xff0c;重写commit message&#xff0c;改完后…

软考复习之软件工程篇

软件生命周期 问题定义&#xff1a;要示系统分析员与用户进行交流&#xff0c;弄清”用户需要计算机解决什么问题”然后提出关于“系统目标与范围的说明”&#xff0c;提交用户审查和确认 可行性研究&#xff1a;一方面在于把待开发的系统的目标以明确的语言描述出来&#xf…

httpClient忽略https的证书认证

忽略https证书认证代码: /*** 创建模拟客户端&#xff08;针对 https 客户端禁用 SSL 验证&#xff09;* return* throws Exception*/public static CloseableHttpClient createHttpClientWithNoSsl() throws Exception {// Create a trust manager that does not validate cer…

【C++】初步认识基于C的优化

C祖师爷在使用C语言时感觉到了不方便的一些点&#xff0c;于是一步一步改进优化&#xff0c;最后形成了C 本文将盘点一下基于C的语法优化 目录 命名空间&#xff1a;命名空间定义&#xff1a;命名空间使用&#xff1a; C输入&输出&#xff1a;cout&#xff1a;endl&#…

司铭宇老师:门店服装销售技巧培训:卖衣服销售方法和技巧

门店服装销售技巧培训&#xff1a;卖衣服销售方法和技巧 在服装零售行业&#xff0c;销售方法和技巧对于提升销售业绩和增强顾客满意度至关重要。一个成功的销售人员需要掌握如何吸引顾客、如何展示商品、如何促成交易等多方面的技能。以下是关于卖衣服的销售方法和技巧的详细…

ai智能写作软件有分享吗?分享4款解放双手的软件!

随着人工智能技术的不断发展&#xff0c;AI智能写作软件逐渐成为内容创作者们的新宠。这些软件不仅能够帮助我们快速生成高质量的文本内容&#xff0c;还能在优化搜索引擎排名等方面发挥重要作用。本文将为大家介绍几款常用的AI智能写作软件&#xff0c;让您轻松提升内容创作效…

如何在飞书创建企业ChatGPT智能问答助手应用并实现公网远程访问(1)

文章目录 前言环境列表1.飞书设置2.克隆feishu-chatgpt项目3.配置config.yaml文件4.运行feishu-chatgpt项目5.安装cpolar内网穿透6.固定公网地址7.机器人权限配置8.创建版本9.创建测试企业10. 机器人测试 前言 在飞书中创建chatGPT机器人并且对话&#xff0c;在下面操作步骤中…