C++ 需要获取数组中最小三个数字的帮助吗
对于该程序,用户必须输入10名选手以及他们完成游泳比赛所需的秒数。我的问题是我必须输出第一、第二和第三名,所以我需要得到三个最小的数组(因为它们是最快的时间),但我不确定如何做到这一点。这是到目前为止我的代码C++ 需要获取数组中最小三个数字的帮助吗,c++,C++,对于该程序,用户必须输入10名选手以及他们完成游泳比赛所需的秒数。我的问题是我必须输出第一、第二和第三名,所以我需要得到三个最小的数组(因为它们是最快的时间),但我不确定如何做到这一点。这是到目前为止我的代码 string names[10] = {}; int times[10] = { 0 }; int num[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int min1 = 0, min2 = 0, min3 = 0; cout << "
string names[10] = {};
int times[10] = { 0 };
int num[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int min1 = 0, min2 = 0, min3 = 0;
cout << "\n\n\tCrawl";
for (int i = 0; i < 10; i++)
{
cout << "\n\n\tPlease enter the name of contestant number " << num[i] << ": ";
cin >> names[i];
cout << "\n\tPlease enter the time it took for them to complete the Crawl style: ";
cin >> times[i];
while (!cin)
{
cout << "\n\tError! Please enter a valid time: ";
cin.clear();
cin.ignore();
cin >> times[i];
}
if (times[i] < times[min1])
min1 = i;
cout << "\n\n\t----------------------------------------------------------------------";
}
system("cls");
cout << "\n\n\tThe top three winners of the Crawl style race are as follows";
cout << "\n\n\t1st Place - " << names[min1];
cout << "\n\n\t2nd Place - " << names[min2];
cout << "\n\n\t3rd Place - " << names[min3];
}
_getch();
return 0;
}
字符串名[10]={};
整数倍[10]={0};
int num[10]={1,2,3,4,5,6,7,8,9,10};
int min1=0,min2=0,min3=0;
cout倍[i];
}
if(次[i]<次[min1])
min1=i;
cout这不是你问题的完整解决方案,只是想为你指明正确的方向
#include <iostream>
#include <limits>
#include <algorithm>
using namespace std;
template <int N>
struct RememberNsmallest {
int a[N];
RememberNsmallest() { std::fill_n(a,N,std::numeric_limits<int>::max()); }
void operator()(int x){
int smallerThan = -1;
for (int i=0;i<N;i++){
if (x < a[i]) { smallerThan = i; break;}
}
if (smallerThan == -1) return;
for (int i=N-1;i>smallerThan;i--){ a[i] = a[i-1]; }
a[smallerThan] = x;
}
};
int main() {
int a[] = { 3, 5, 123, 0 ,-123, 1000};
RememberNsmallest<3> rns;
rns = std::for_each(a,a+6,rns);
std::cout << rns.a[0] << " " << rns.a[1] << " " << rns.a[2] << std::endl;
// your code goes here
return 0;
}
由于您还需要知道最佳时间的名称,因此您应该使用
struct TimeAndName {
int time;
std::string name;
}
并将上述函子更改为使用TimeAndName
而不是int
,并使其也记住名称。。。或者提出不同的解决方案;),但在任何情况下,都应该使用类似于TimeAndName
的结构
当您的数组很小时,您甚至可以考虑使用<代码> STD::vector < /代码>,并通过<代码> STD::排序< < />代码>使用您的自定义<代码>时间和名称::运算符< p>您的代码中满是错误:
只要不分配min2和min3,您会如何处理它们??他们总是0
试着检查:cout我希望这会给你预期的输出。但是我建议你使用一些排序算法,比如冒泡排序,快速排序等等
#include <iostream>
#include<string>
using namespace std;
int main() {
int times[10] = { 0 };
int num[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int min1 = 0, min2 = 0, min3 = 0,m;
string names[10] ;
cout << "\n\n\tCrawl";
for (int i = 0; i < 10; i++)
{
cout << "\n\n\tPlease enter the name of contestant number " << num[i] << ": ";
cin >> names[i];
cout << names[i];
cout << "\n\tPlease enter the time it took for them to complete the Crawl style: ";
cin >> times[i];
cout<<times[i];
while (!cin)
{
cout << "\n\tError! Please enter a valid time: ";
cin.clear();
cin.ignore();
cin >> times[i];
}
if(times[i]==times[min1]){
if(times[min1]==times[min2]){
min3=i;
}else{min2 =i;}
}else if(times[i]==times[min2]){
min3=i;
}
if (times[i] < times[min1]){
min1 = i;
cout <<i;
}
int j=0;
while(j<i){
if((times[j]>times[min1])&&(times[j]<times[min2])){
min2 =j;
j++;
}
j++;
}
m=0;
while(m<i){
if((times[m]>times[min2])&&(times[m]<times[min3])){
min3 =m;
m++;
}
m++;
}
cout << "\n\n\t----------------------------------------------------------------------";
}
cout << "\n\n\tThe top three winners of the Crawl style race are as follows";
cout << "\n\n\t1st Place - " << names[min1];
cout << "\n\n\t2nd Place - " << names[min2];
cout << "\n\n\t3rd Place - " << names[min3];
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
整数倍[10]={0};
int num[10]={1,2,3,4,5,6,7,8,9,10};
int min1=0,min2=0,min3=0,m;
字符串名[10];
实际上,标准库中有一种算法可以完全满足您的需要:std::partial_sort
。正如其他人之前指出的那样,要使用它,您需要将所有参与者数据放在一个结构中
因此,首先定义一个包含所有相关数据的结构。因为在我看来,您只使用参赛者的数量,以便以后能够以最快的时间找到游泳运动员的名字,所以我会将其删除。当然,如果您愿意,您也可以将其重新添加
struct Swimmer {
int time;
std::string name;
};
因为您知道一场比赛中总有10名参赛者,所以您也可以用std::array
替换C样式的数组
然后,要在用户中读取的代码可能如下所示:
std::array<Swimmer, 10> participants;
for (auto& participant : participants) {
std::cout << "\n\n\tPlease enter the name of the next contestant: ";
std::cin >> participant.name;
std::cout << "\n\tPlease enter the time it took for them to complete the Crawl style: ";
while(true) {
if (std::cin >> participant.time) {
break;
}
std::cout << "\n\tError! Please enter a valid time: ";
std::cin.clear();
std::cin.ignore();
}
std::cout << "\n\n\t----------------------------------------------------------------------";
}
std::数组参与者;
用于(自动和参与者:参与者){
std::cout>participant.name;
标准::cout>participant.time){
打破
}
std::cout获取所有值、对它们进行排序并获取前三个元素如何?值必须由用户输入。您拥有的是3个大小相同的数组,请尝试创建一个包含您的姓名、时间(以及任何其他信息)的结构然后对这些值进行排序。这样,您的排序可以将名称和其他数据保存在一起。那么呢?如果用户必须输入值,则无所谓。让用户输入所有值,然后进行排序并获取三个最小的数字。还有其他方法,例如获取最小的数字,从输入中删除它,然后执行相同的两个操作我的答案不是你想要的答案吗?
struct Swimmer {
int time;
std::string name;
};
std::array<Swimmer, 10> participants;
for (auto& participant : participants) {
std::cout << "\n\n\tPlease enter the name of the next contestant: ";
std::cin >> participant.name;
std::cout << "\n\tPlease enter the time it took for them to complete the Crawl style: ";
while(true) {
if (std::cin >> participant.time) {
break;
}
std::cout << "\n\tError! Please enter a valid time: ";
std::cin.clear();
std::cin.ignore();
}
std::cout << "\n\n\t----------------------------------------------------------------------";
}
std::partial_sort(std::begin(participants),
std::begin(participants) + 3,
std::end(participants),
[] (auto const& p1, auto const& p2) { return p1.time < p2.time; });
std::cout << "\n\n\tThe top three winners of the Crawl style race are as follows";
std::cout << "\n\n\t1st Place - " << participants[0].name;
std::cout << "\n\n\t2nd Place - " << participants[1].name;
std::cout << "\n\n\t3rd Place - " << participants[2].name << std::endl;