You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++双队列处理问题:遍历顺序错误与输出末尾多余逗号修复

修复方案

1. 修复compsci类书籍顺序错误

原代码先后完整遍历队列1、队列2,不符合交替检查两个队列队首的处理规则,将原代码中两个独立的遍历队列1、队列2的while循环,替换为如下交替处理逻辑:

// 替换原两个遍历queue1、queue2的独立while循环
while (!queue1.empty() || !queue2.empty()) {
    // 先处理队列1队首元素
    if (!queue1.empty()) {
        if (queue1.front().find("compsci") != string::npos) {
            sol.push_back(queue1.front());
            cost += 20;
        } else {
            queue3.push(queue1.front());
            cost += 10;
        }
        queue1.pop();
    }
    // 再处理队列2队首元素
    if (!queue2.empty()) {
        if (queue2.front().find("compsci") != string::npos) {
            sol.push_back(queue2.front());
            cost += 20;
        } else {
            queue3.push(queue2.front());
            cost += 10;
        }
        queue2.pop();
    }
}

2. 修复输出列表末尾多余逗号

原输出逻辑每输出一个书名就追加逗号,改为仅在非第一个书名前插入逗号,修改输出部分代码如下:

// 替换原遍历输出sol的代码
for (int i = 0; i < sol.size(); i++) {
    if (i != 0) {
        output << ",";
    }
    output << sol[i];
}

修改后完整主代码

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include "ArgumentManager.h"

using namespace std;

queue<string> splitStringUsingComma(string text)
{
  queue<string> sol;
  stringstream ss(text);
  while(ss.good())
  {
    string x;
    getline(ss,x,',');
    sol.push(x);
  }
  return sol;
}

int main(int argc, char* argv[])
{
  ArgumentManager am(argc, argv);
  ifstream input;

  string infileName = am.get("input");
  string outfileName = am.get("output");
  input.open(infileName);

  string text;
  queue<string> queue1,queue2;
  getline(input,text);
  queue1 = splitStringUsingComma(text);
  text.clear();
  getline(input,text);
  queue2 = splitStringUsingComma(text);
  queue<string> queue3;
  int cost{0};
  vector<string> sol;

  while (!queue1.empty() || !queue2.empty()) {
      if (!queue1.empty()) {
          if (queue1.front().find("compsci") != string::npos) {
              sol.push_back(queue1.front());
              cost += 20;
          } else {
              queue3.push(queue1.front());
              cost += 10;
          }
          queue1.pop();
      }
      if (!queue2.empty()) {
          if (queue2.front().find("compsci") != string::npos) {
              sol.push_back(queue2.front());
              cost += 20;
          } else {
              queue3.push(queue2.front());
              cost += 10;
          }
          queue2.pop();
      }
  }

  while(!queue3.empty())
  {
    sol.push_back(queue3.front());
    queue3.pop();
    cost+=20;
  }

  ofstream output;
  output.open(outfileName);
  output<<cost<<'\n';

  for (int i = 0; i < sol.size(); i++) {
      if (i != 0) {
          output << ",";
      }
      output << sol[i];
  }

  output<<endl;
  input.close();
  output.close();
  return 0;
}

内容的提问来源于stack exchange,提问作者TinnyMintz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 04:15:03