当前位置:   article > 正文

53 ABR 车路协同场景_abr 车路协同场景 c++实现

abr 车路协同场景 c++实现

#include <iostream>
#include <sstream>
#include <vector>
#include <regex>
#include <string>

using namespace std;

int main() 
{
    // 创建一个字符串,用于存储输入
    string input;
    // 从标准输入读取一行
    getline(cin, input);
    // 创建一个正则表达式,用于匹配输入
    regex re("A=\\{(.+)},B=\\{(.+)},R=(.+)");
    smatch match;

    // 检查输入是否匹配正则表达式
    if (regex_search(input, match, re)) 
    {
        // 从匹配结果中提取事件、设备和范围
        istringstream eventsStream(match[1].str());
        istringstream devicesStream(match[2].str());
        int range = stoi(match[3].str());

        vector<int> events, devices;
        string temp;

        while (getline(eventsStream, temp, ',')) 
        {
            events.push_back(stoi(temp));
        }

        while (getline(devicesStream, temp, ',')) 
        {
            devices.push_back(stoi(temp));
        }

        // 创建一个字符串流,用于存储结果对
        ostringstream resultPairs;

        // 遍历每个事件
        for (int event : events) 
        {
            int count = 0;
            // 遍历每个设备
            for (int device : devices) 
            {
                // 如果设备小于事件,则继续下一个设备
                if (device < event) continue;
                // 如果设备与事件的距离小于等于范围,或者这是第一个设备,则将其添加到结果对中
                if (device - event <= range || count == 0) 
                {
                    resultPairs << "(" << event << "," << device << ")";
                    count++;
                } 
                else 
                {
                    // 否则,跳出循环
                    break;
                }
            }
        }

        // 打印结果对
        cout << resultPairs.str() << endl;
    }

    return 0;
}
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/705243
推荐阅读
相关标签
  

闽ICP备14008679号