需要帮助获取数组中最小的三个数字
Need help on getting the smallest three numbers on an array
对于这个程序,用户必须输入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 << "nntCrawl";
for (int i = 0; i < 10; i++)
{
cout << "nntPlease enter the name of contestant number " << num[i] << ": ";
cin >> names[i];
cout << "ntPlease enter the time it took for them to complete the Crawl style: ";
cin >> times[i];
while (!cin)
{
cout << "ntError! Please enter a valid time: ";
cin.clear();
cin.ignore();
cin >> times[i];
}
if (times[i] < times[min1])
min1 = i;
cout << "nnt----------------------------------------------------------------------";
}
system("cls");
cout << "nntThe top three winners of the Crawl style race are as follows";
cout << "nnt1st Place - " << names[min1];
cout << "nnt2nd Place - " << names[min2];
cout << "nnt3rd Place - " << names[min3];
}
_getch();
return 0;
}
如您所见,它是不完整的。我知道怎么求最小的数,但是第二和第三小的数给我带来了麻烦。
你的代码充满了错误:
如果你不给min2和min3赋值,你会怎么处理它们呢?它们总是0尝试检查:cout << min2 << " " << min3;
你也不能像那样初始化字符串数组
为什么你使用整数数组来打印输入的数字:num吗?相反,你可以在循环中使用I,每次
都给它加1使用一个好的方法来解决你的问题,所以考虑使用结构/类:
struct Athlete { std::string name; int time; }; int main() { Athlete theAthletes[10]; for(int i(0); i < 10; i++) { std::cout << "name: "; std::getline(std::cin, theAthletes[i].name); std::cin.sync(); // flushing the input buffer std::cout << "time: "; std::cin >> theAthletes[i].time; std::cin.sync(); // flushing the input buffer } // sorting athletes by smaller time for(i = 0; i < 10; i++) for(int j(i + 1); j < 10; j++) if(theAthletes[i].time > theAthletes[j].time) { Athlete tmp = theAthletes[i]; theAthletes[i] = theAthletes[j]; theAthletes[j] = tmp; } // printing the first three athletes std::cout << "the first three athelets:nn"; std::cout << theAthletes[0].name << " : " << theAthletes[0].time << std::endl; std::cout << theAthletes[1].name << " : " << theAthletes[1].time << std::endl; std::cout << theAthletes[2].name << " : " << theAthletes[2].time << std::endl; return 0; }
我希望这会给你预期的输出。但我建议你使用一些排序算法,如冒泡排序,快速排序等。
#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 << "nntCrawl";
for (int i = 0; i < 10; i++)
{
cout << "nntPlease enter the name of contestant number " << num[i] << ": ";
cin >> names[i];
cout << names[i];
cout << "ntPlease enter the time it took for them to complete the Crawl style: ";
cin >> times[i];
cout<<times[i];
while (!cin)
{
cout << "ntError! 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 << "nnt----------------------------------------------------------------------";
}
cout << "nntThe top three winners of the Crawl style race are as follows";
cout << "nnt1st Place - " << names[min1];
cout << "nnt2nd Place - " << names[min2];
cout << "nnt3rd Place - " << names[min3];
return 0;
}
实际上在标准库中有一个算法可以完全满足您的需求: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 << "nntPlease enter the name of the next contestant: ";
std::cin >> participant.name;
std::cout << "ntPlease enter the time it took for them to complete the Crawl style: ";
while(true) {
if (std::cin >> participant.time) {
break;
}
std::cout << "ntError! Please enter a valid time: ";
std::cin.clear();
std::cin.ignore();
}
std::cout << "nnt----------------------------------------------------------------------";
}
部分排序现在基本上是一行代码:
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 << "nntThe top three winners of the Crawl style race are as follows";
std::cout << "nnt1st Place - " << participants[0].name;
std::cout << "nnt2nd Place - " << participants[1].name;
std::cout << "nnt3rd Place - " << participants[2].name << std::endl;
完整的工作代码可以在coliru上找到。
这不是你的问题的完整解决方案,但只是为了指出你进入正确的方向…
#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;
}
打印
-123 0 3
由于您还需要知道最佳时间的名称,因此您应该使用
struct TimeAndName {
int time;
std::string name;
}
并将上述函子改为TimeAndName
而不是int
,并使其也记住名称…或想出一个不同的解决方案;),但在任何情况下,您应该使用类似于TimeAndName
的结构体。
由于您的数组相当小,您甚至可以考虑使用std::vector<TimeAndName>
,并使用自定义的TimeAndName::operator<
通过std::sort
进行排序。
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 使用三个数字比较器进行排序
- 有没有更简单的方法可以从用户那里获取三个数字并按升序打印它们?
- 数组中最大的三个数字
- 跳过每三个数字
- 三个数字之间的 C++ 相加两个更高的数字,没有循环和数组
- 如何使用不同的功能计算三个数字的总和,平均值和产物
- 创建一个程序,要求用户输入两个数字,并有三个可能的答案.代码未编译.数字(作为int)
- 为什么linear_congruenty_engine :: seed(sseq)丢弃了由种子序列生成的三个数字
- 根据列中的第三个数字对向量进行排序
- 三个数字彩票猜谜游戏C++
- 从一组数字中找出最接近的三个数字值
- 需要帮助获取数组中最小的三个数字
- 输入三个数字,并在c++中确定最高和最低的数字
- 三个不同的数字
- 为什么我在尝试输出UTF-8字符时会得到三个不同的数字
- 至少三个数字