gpt4 book ai didi

c++ - 如何找到工作之间的反向关系?

转载 作者:行者123 更新时间:2023-11-28 08:16:59 25 4
gpt4 key购买 nike

我一直在努力寻找工作之间的反向关系。更具体地说,我会用一个例子来说明。

假设我有 n 个工作,即 {0,1,2,3,4,...n}。我也有工作之间的关系。我只知道后续工作,即 2 后跟 4,55 之后是 7,8 等。我把它放在一个文本文件中。我想获得工作之间的优先关系(5 的前任工作是什么?)。

有文本输出会很棒。我有一些代码,但它不起作用。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

#define TOTAL_ACTIVITY 123

void readFile();
void change ();
void writeFile ();

struct Activity {
int precedessor [3];
int successor [3];
int id;
};

Activity activityList[TOTAL_ACTIVITY];

void main() {
readFile();
change();
writeFile();
}

void readFile() {
ifstream myReadFile;
myReadFile.open("pre.txt");
if (!myReadFile) { //check whether the file can be opened
cerr << "Unable to open file"; // terminate with error
}

while (!myReadFile.eof()) {
int Id,suc1, suc2, suc3;
int t = 0;
while (myReadFile >> Id >> suc1 >> suc2 >> suc3) //data should be in this order
{
activityList[t].id = Id;
activityList[t].successor [0] = suc1;
activityList[t].successor [1] = suc2;
activityList[t].successor [2] = suc3;
t++;
}
}
return;
}

void change() {
int act;
for (int i=1;i<TOTAL_ACTIVITY;i++){
for (int j=0;j<TOTAL_ACTIVITY;j++){
for (int k=0;k<3;k++) {
if (activityList[j].successor[k]==i;)
}
}
}
}

void writeFile() {
ofstream out("out.txt");
out << "id\t" << "Pre1\t" << "Pre2\t" << "Pre3\t"<<"\n";
for (int j = 0; j < TOTAL_ACTIVITY; j++) {
out << activityList[j].id << "\t";
out << activityList[j].precedessor[0]<< "\t";
out << activityList[j].precedessor[1] << "\t";
out << activityList[j].precedessor[2] << "\t";
out << "\n";
}
out.close();
}

这是一个示例输入:

ID  Successor1  Successor2  Successor3
1 2 3 4
2 6 11 15
3 7 8 13
4 5 9 10
5 20
6 30
7 27
8 12 19 27
9 14
10 16 25
11 20 26
12 14
13 17 18
14 17
15 25
16 21 22
17 22
18 20 22
19 24 29
20 23 25
21 28
22 23
23 24
24 30
25 30
26 31
27 28
28 31
29 32
30 32
31 32

输出应该是这样的:

Id Predecesor1  Predecesor2  Predecesor3
........................................
...........................................
...........................................

最佳答案

您已获得工作的继任者,但您无需保留此信息。

例如,如果我说:5 -> 6, 7 表示 5 后跟 67,那么就相当于说67前面有5,对吧。

然后您可以:

  • 读取后继时直接输出优先级
  • 存储在关联容器中,使用作业 ID 作为键,前任作为值

具体细节...供您锻炼以完成您的作业。

关于c++ - 如何找到工作之间的反向关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7401021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com