如何在二维结构中删除元素进行比较



我接到一个绝密组织的负责人的任务,收集退休特工的统计数据有几个秘密机构雇用间谍。是时候让这些机构的雇员退休了

给定任务:组织本身由两个结构组成。

  1. 机构其中包括string-titleint-min_missionint-agent_count和Spy* agents
  2. 间谍值为namestring-specialityint-completed_missionsbool-is_private。我想指出的是,间谍只有3个专业类别("外交"、"专业"、"秘密")

Job Purpose:需要找到一个释放座席数量最多的代理。

为了让他们辞职,机构必须检查几个双关语,超过这些后,其职位被解除。这些规则如下:

  1. 他完成的任务数量必须超过他的机构设定的最低限度。
  2. 如果特工的专业是"专业",他的最少任务数比其他人少2个。
  3. 如果一个特工的专业是"黑暗神秘",他的最小任务数量是其他人的两倍。
  4. 如果代理为Private(即逻辑类型is_private为== 1),则永远不会释放该代理

测试输入:

3对于代理商,1' '是代理商的名称,2' '是最小数量限制,3' '是代理商的数量。//"对间谍来说,第一是名字,第二是专长,第三是完成的任务">

血蜻蜓5 6

  1. Jagternaut Diplomacy 70
  2. 暗黑秘境6 0
  3. 萨鲁曼外交3 1
  4. Coltosus SpecOps 10
  5. Samtanta darkmystery 11 0
  6. Tantos SpecOps 4 1

GoldenNose 6 4

  1. Smith SpecOps 8 1
  2. Smity DarkMistery 11 0
  3. Smitens外交5 0
  4. Smaut darkmystery 16 0

Lucaduca 10 5

  1. 马里奥SpecOps 4 0
  2. 里约外交90
  3. 卢奇奥外交11
  4. 朱丽叶外交14
  5. Bob DarkMistery 35 1

应该在监视器上显示的值或输出:Lucaduca


简而言之,我编写了一个命令,用于分类哪些代理可以被释放。

但是我忘记了如何删除数组中通过检查的代理,以便数组的大小发生变化。(忽略变量->总和)

这是我的代码。

#include "iostream"
#include "string"
using namespace std;
struct Spy { //The Spy structure
string name;
string speciality;
int missions;
bool is_private;
};
struct Agency { //The agency structure
string title;
int min_missions;
int agentCount;
Spy* agnt;
};
istream& operator >> (istream& is, Spy& sp) { //The operator to enter the values of spy
is >> sp.name >> sp.speciality >> sp.missions >> sp.is_private;
return is;
}
istream& operator >> (istream& is, Agency& agc) {//To enter the values of Agency
is >> agc.title >> agc.min_missions >> agc.agentCount;
return is;
}

int main() {
int N; cout << "Enter the number of agencies: n";
cin >> N; //
Agency* agc = new Agency[N];
for (int i = 0; i < N; ++i) { //Creating a loop for the Agency struct, which would store the values of an individual agency
cin >> agc[i];
agc[i].agnt = new Spy[agc[i].agentCount];
for (int j = 0; j < agc[i].agentCount; ++j) { //This is for the struct Spy that will be stored inside the agentCount
cin >> agc[i].agnt[j];
}
}
for (int i = 0; i < N; ++i) {
int sum = 0;
int count = 0;
for (int j = 0; j < agc[i].agentCount; ++j) {
if (agc[i].agnt[j].is_private) { //Сonditional operator to check whether the agent is private or not
continue;
}
else {
if (agc[i].agnt[j].speciality == "DarkMistery") {//To check the speciality of agent
count = (agc[i].min_missions * 2);//If a spy's specialization is DarkMistery,
// he will get double the minimum number of missions than others
}
else if (agc[i].agnt[j].speciality == "SpecOps") {//If a spy's specialization is SpecOps,
count = (agc[i].min_missions - 2); //he/she will get  2 fewer mission than the others
}
else { //f a spy's specialization belongs to the third category
//Nothing will change for him/her
count = agc[i].min_missions;
}
if (agc[i].agnt[j].missions > count) {
sum++;
} else {
continue;
}
}
}
}
}

我试图通过sum来确定非豁免代理人数最多的机构。但每次通过下一个机构,它都会更新。我尝试使用delete命令删除spy值,但是我不知道如何在二维结构上使用它我希望看到数组agc[I].agentCount发生变化当然,如果适合这段代码,它的大小是动态改变的。

如果您只想跟踪代理机构应该发布的代理数量,那么您几乎完成了:

  • 您可以使用从座席名称到待释放座席数量的映射。
  • 然后,在最后两个嵌套循环中:
    • 在内环开始前,重置一个需要释放的座席计数。
    • 每一次内循环的迭代,你更新计数器。
    • 内循环结束后,在地图中插入一个新元素,即座席名称和待释放座席数。

下面的代码做到了这一点,尽管我不敢说输出与预期不同(BloodDragonfly而不是Lucaduca)。这可能与输入数据或算法有关,以决定是否必须释放代理。

(演示)

using map_of_agents_to_be_released_per_agency_t = std::unordered_map<std::string, int>;
map_of_agents_to_be_released_per_agency_t agents_to_be_released{};
for (int i = 0; i < no_of_agencies; ++i) {
auto& agency{ agencies[i] };
int no_of_agents_to_be_released = 0;
for (int j = 0; j < agency.agent_count; ++j) {
auto& agent{ agency.agents[j] };
if (not agent.is_private) {
if (agent.speciality == "DarkMistery") {
if (agent.missions > agency.min_missions * 2) {
no_of_agents_to_be_released++;
}
} else if (agent.speciality == "SpecOps") {
if (agent.missions > agency.min_missions - 2) {
no_of_agents_to_be_released++;
}
} else if (agent.speciality == "Diplomacy") {
if (agent.missions > agency.min_missions) {
no_of_agents_to_be_released++;
}
}
}
}
agents_to_be_released.insert({agency.title, no_of_agents_to_be_released});
}
fmt::print("Agents to be released: {}n", agents_to_be_released);
fmt::print("Agency with more agents to be released: {}n",
std::ranges::max_element(
agents_to_be_released,
std::less<>{},
&map_of_agents_to_be_released_per_agency_t::value_type::second
)->first);
// Outputs:
//
//   Agents to be released: {"Lucaduca": 0, "GoldenNose": 1, "BloodDragonfly": 2}
//   Agency with more agents to be released: BloodDragonfly

最新更新