【问题标题】:Sorting array and saving the old index in C++在 C++ 中对数组进行排序并保存旧索引
【发布时间】:2013-03-28 22:18:17
【问题描述】:

我正在做在线挑战,必须做到以下几点。

村子里有一场比赛。所以在第一行输入两个数字 N(即有多少人将参加比赛)和 K(其中有多少人可以进入第 2 阶段)。

之后,我们为两个阶段的每个候选人输入 N 次选票。

示例输入:

5 3
9 2
3 10
5 6
8 4
6 5

如您所见,我们输入了N=5K=3,这意味着 5 个候选者,因此增加了 5 行,其中 3 行进入第 2 阶段。

在我们对数组进行排序后,得票最多的候选人是 6、8 和 9 的候选人。所以他们将进入第 2 阶段。获胜者是在第 2 阶段中得票最多的候选人。在这种情况下,6 有 5 票,这是最多的(8 有 4,9 有 2),因此我们输出 6 的索引为 5。

到目前为止我得到了什么:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int arr[50],nizabackup[50],n,k,niza2[50],saveindex[50],indexp=0;
    cin >> n >> k;
    for(int i=0;i<n;i++)
    {
        cin >> arr[i] >> niza2[i];
        nizabackup[i] = arr[i];
    }
    sort(arr,arr+n);
    for(int j=n;j>=k;j--)
    {
        for(int k=0;k<n;k++)
        {
            if(arr[j]==nizabackup[k])
            {
                saveindex[0]=k;
                indexp++;
            }
        }
    }
    sort(saveindex,saveindex+indexp);
    cout << saveindex[indexp];
    return 0;
}

我需要提示该怎么做以及其他问题——为什么我的调试器没有读取第二个 for 循环?

【问题讨论】:

  • 看来您可能希望在第二个for 循环中使用j &gt;= k,因为n 大于k
  • 你是对的。谢谢!

标签: c++ arrays


【解决方案1】:

好的,替代实现。还有更多设置,但请先阅读main,看看实际逻辑有多简单。

#include <vector>
#include <iostream>
#include <algorithm>

struct Contestant {
    int index;
    int firstVote;
    int secondVote;
    Contestant(int i, int v1, int v2) : index(i), firstVote(v1), secondVote(v2)
    {}
};
// return true if l should come before r in sorted result
bool byFirstVote(Contestant const &l, Contestant const &r) {
    return l.firstVote > r.firstVote; // sort by first vote
}
bool bySecondVote(Contestant const &l, Contestant const &r) {
    return l.secondVote > r.secondVote; // sort by second vote
}

int main()
{
    int n, k;
    std::cin >> n >> k;
    // populate a single vector of {index, firstVote, secondVote}
    std::vector<Contestant> contestants;
    for(int i=0; i<n; i++) {
        int v1, v2;
        std::cin >> v1 >> v2;
        contestants.push_back(Contestant(i+1, v1, v2));
    }
    // sort all by firstVote, then sort first k by secondVote
    std::sort(contestants.begin(), contestants.end(), byFirstVote);
    std::sort(contestants.begin(), contestants.begin()+k, bySecondVote);
    std::cout << contestants.front().index << std::endl;
}

我将索引(根据您的示例从 1 开始,而不是 0)和两个投票都存储在一个结构中。

然后,我只需更改排序的字段。

【讨论】:

  • 啊。这让我意识到我的回答存在问题。 +1
  • 哇,老实说,很好的实现。我认为向量在某种程度上比数组更有用。无论如何,我有几个问题要问。以下代码有什么作用 Contestant(int i, int v1, int v2) : index(i), firstVote(v1), secondVote(v2) ?为什么我们下面还有额外的{}?我们究竟在布尔函数中检查什么?提前致谢。
  • @JohnSmith Contestant(int i, int v1, int v2) : index(i), firstVote(v1), secondVote(v2) 是一个初始化列表,它与Contestant(int i, int v1, int v2) {index=i;firstVote=v1;secondVote=v2;} 类似~,初始化列表上的空{} 是函数的空体。在函数体运行之前处理一个初始化列表。
  • 我明白了!而bool函数我不明白l或r是从哪里来的,我们在哪里使用呢?
  • 所以std::sort 的(可选)第三个参数是一个带有两个参数的比较器(常量引用你正在排序的任何类型 - 这里我们正在排序Contestants,所以它们是@987654330 @) 并返回一个布尔值。如果左边的比较数应该在排序集中的右边之前,则布尔值应该是true,否则为false。没有第三个参数的std::sort 只使用operator&lt;,因此默认情况下按升序排序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 2017-04-28
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 2016-01-01
  • 2011-08-03
相关资源
最近更新 更多