如何按降序对两个数组进行排序?
How can I sort two arrays in descending order?
我正在尝试编写一个程序,要求用户输入 10 个不同的人(第 1 个人、第 2 个人、...、第 10 个人)早餐吃的煎饼数量。
我需要修改程序,让它按照所有 10 个人吃的煎饼数量的顺序输出一个列表。
示例:
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes
我已经能够将煎饼的数量按降序排列,但是我很难将正确的 Person
分配给他们的煎饼数量。
到目前为止,这是我的代码:
int main()
{
int person[10];
int i;
int input;
int n = sizeof(person) / sizeof(person[0]);
// store the number entered by the user in each array element
for(i = 0; i < 10; ++i)
{
cout << "How many pancakes did Person " << i + 1 << " eat? ";
cin >> person[i];
}
cout << endl;
cout << endl;
sort(person, person + n, greater<int>()); // sorts array in descending order. "greater()" puts larger numbers first
for(i = 0; i < n; i++)
{
cout << "Person " << i + 1 << " ate " << person[i] << " pancakes." << endl;
}
return 0;
}
如有任何帮助,我们将不胜感激!
您可以使用std::pair
的std::vector
来存储Person索引及其对应的值。然后,您可以使用比较器函数按值排序。
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<pair<int, int>> person(10);
// store the number entered by the user in each array element
for(int i = 0; i < person.size(); ++i)
{
person[i].first = i + 1;
cout << "How many pancakes did Person " << i + 1 << " eat? ";
cin >> person[i].second;
}
cout << endl;
cout << endl;
auto compare = [](const pair<int, int>& a, const pair<int, int>& b)
{
return a.second > b.second;
};
sort(person.begin(), person.end(), compare);
for(int i = 0; i < person.size(); i++)
{
cout << "Person " << person[i].first << " ate " << person[i].second << " pancakes." << endl;
}
return 0;
}
您还可以将人员详细信息封装在 struct
中,并将 struct
与 std::vector
一起使用:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct Person
{
int index;
int pancakesEaten;
};
int main()
{
vector<Person> person(10);
// store the number entered by the user in each array element
for(int i = 0; i < person.size(); ++i)
{
person[i].index = i + 1;
cout << "How many pancakes did Person " << i + 1 << " eat? ";
cin >> person[i].pancakesEaten;
}
cout << endl;
cout << endl;
auto compare = [](const Person& a, const Person& b)
{
return a.pancakesEaten > b.pancakesEaten;
};
sort(person.begin(), person.end(), compare);
for(int i = 0; i < person.size(); i++)
{
cout << "Person " << person[i].index << " ate " << person[i].pancakesEaten << " pancakes." << endl;
}
return 0;
}
您可以利用 std::map 的排序。
int main()
{
constexpr int person_num = 10;
multimap<int, int, greater<>> pancakes_person_pairs;
for (int i = 0; i < person_num; ++i)
{
int cur_person = i + 1;
cout << "How many pancakes did Person " << cur_person << " eat? ";
unsigned int cur_pancakes;
cin >> cur_pancakes;
pancakes_person_pairs.insert(make_pair(cur_pancakes, cur_person));
}
cout << endl << endl;
for (const auto& pancakes_person_pair : pancakes_person_pairs)
{
cout << "Person " << pancakes_person_pair.second << " ate " << pancakes_person_pair.first << " pancakes." << endl;
}
return 0;
}
使用间接。设置索引 0 到 n 的数组,并通过推迟与计数数组的比较来对该索引数组进行排序:
vector<int> ind(n);
iota(ind.begin(), ind.end(), 0); // #include <numeric>
sort(ind.begin(), ind.end(),
[&](int a, int b) { return person[a] > person[b]; });
for (int i : ind) {
cout << "Person " << i + 1 << " ate " << person[i] << " pancakes." << endl;
}
}
我正在尝试编写一个程序,要求用户输入 10 个不同的人(第 1 个人、第 2 个人、...、第 10 个人)早餐吃的煎饼数量。
我需要修改程序,让它按照所有 10 个人吃的煎饼数量的顺序输出一个列表。
示例:
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes
我已经能够将煎饼的数量按降序排列,但是我很难将正确的 Person
分配给他们的煎饼数量。
到目前为止,这是我的代码:
int main()
{
int person[10];
int i;
int input;
int n = sizeof(person) / sizeof(person[0]);
// store the number entered by the user in each array element
for(i = 0; i < 10; ++i)
{
cout << "How many pancakes did Person " << i + 1 << " eat? ";
cin >> person[i];
}
cout << endl;
cout << endl;
sort(person, person + n, greater<int>()); // sorts array in descending order. "greater()" puts larger numbers first
for(i = 0; i < n; i++)
{
cout << "Person " << i + 1 << " ate " << person[i] << " pancakes." << endl;
}
return 0;
}
如有任何帮助,我们将不胜感激!
您可以使用std::pair
的std::vector
来存储Person索引及其对应的值。然后,您可以使用比较器函数按值排序。
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<pair<int, int>> person(10);
// store the number entered by the user in each array element
for(int i = 0; i < person.size(); ++i)
{
person[i].first = i + 1;
cout << "How many pancakes did Person " << i + 1 << " eat? ";
cin >> person[i].second;
}
cout << endl;
cout << endl;
auto compare = [](const pair<int, int>& a, const pair<int, int>& b)
{
return a.second > b.second;
};
sort(person.begin(), person.end(), compare);
for(int i = 0; i < person.size(); i++)
{
cout << "Person " << person[i].first << " ate " << person[i].second << " pancakes." << endl;
}
return 0;
}
您还可以将人员详细信息封装在 struct
中,并将 struct
与 std::vector
一起使用:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct Person
{
int index;
int pancakesEaten;
};
int main()
{
vector<Person> person(10);
// store the number entered by the user in each array element
for(int i = 0; i < person.size(); ++i)
{
person[i].index = i + 1;
cout << "How many pancakes did Person " << i + 1 << " eat? ";
cin >> person[i].pancakesEaten;
}
cout << endl;
cout << endl;
auto compare = [](const Person& a, const Person& b)
{
return a.pancakesEaten > b.pancakesEaten;
};
sort(person.begin(), person.end(), compare);
for(int i = 0; i < person.size(); i++)
{
cout << "Person " << person[i].index << " ate " << person[i].pancakesEaten << " pancakes." << endl;
}
return 0;
}
您可以利用 std::map 的排序。
int main()
{
constexpr int person_num = 10;
multimap<int, int, greater<>> pancakes_person_pairs;
for (int i = 0; i < person_num; ++i)
{
int cur_person = i + 1;
cout << "How many pancakes did Person " << cur_person << " eat? ";
unsigned int cur_pancakes;
cin >> cur_pancakes;
pancakes_person_pairs.insert(make_pair(cur_pancakes, cur_person));
}
cout << endl << endl;
for (const auto& pancakes_person_pair : pancakes_person_pairs)
{
cout << "Person " << pancakes_person_pair.second << " ate " << pancakes_person_pair.first << " pancakes." << endl;
}
return 0;
}
使用间接。设置索引 0 到 n 的数组,并通过推迟与计数数组的比较来对该索引数组进行排序:
vector<int> ind(n);
iota(ind.begin(), ind.end(), 0); // #include <numeric>
sort(ind.begin(), ind.end(),
[&](int a, int b) { return person[a] > person[b]; });
for (int i : ind) {
cout << "Person " << i + 1 << " ate " << person[i] << " pancakes." << endl;
}
}