要求:有20个长跑队,每队5个人,依次输入各队每个人的身高,然后输出每个队的最高身高
1、C语言实现
/*20190930*/
#include <stdio.h>
#define M 20
#define N 5
int main()
{
float run[M][N];
float height_max[M];
int i, j;
for(i = 0; i < M; i++)
{
printf("Input the height of each team:\n");
for(j = 0; j < N; j++)
{
scanf("%f", &run[i][j]);
printf(" ");
}
}
for(i = 0; i < M; i++)
{
height_max[i] = run[i][0];
}
for(i = 0; i < M; i++)
{
for(j = 0; j < (N-1); j++)
{
if(run[i][j] < run[i][j+1])
height_max[i] = run[i][j+1];
}
printf("Output the heightest of each team: %f\n", height_max[i]);
}
return 0;
}
2、C++实现
#include <iostream>
const int M = 20;
const int N = 5;
int main()
{
using namespace std;
float run[M][N];
float height_max[M];
int i, j;
for(i = 0; i < M; i++)
{
cout << "Input the height of each team:\n";
for(j = 0; j < N; j++)
{
cin >> run[i][j];
cout <<" ";
}
}
for(i = 0; i < M; i++)
{
height_max[i] = run[i][0];
}
for(i = 0; i < M; i++)
{
for(j = 0; j < (N-1); j++)
{
if(run[i][j] < run[i][j+1])
height_max[i] = run[i][j+1];
}
cout << "Output the heightest of each team: " << height_max[i] << endl;
}
return 0;
}
3、利用STL容器set实现(省时),key:height,key若重复则可使用multiset,也可以用map实现,key:height,value:ID
#include <iostream>
#include <set>
using namespace std;
#define QUEUE_NUM 20
#define PEOPLE_NUM 5
int main()
{
set<float> mySet[QUEUE_NUM];
float height = 0;
/*Input Height*/
for(int i = 0; i < QUEUE_NUM; i++)
{
printf("Input the height of each team:\n");
for(int j = 0; j < PEOPLE_NUM; j++)
{
cin >> height;
mySet[i].insert(height);
}
//set<float>::iterator it = mySet[i].end();
cout << "Output the heightest: " << *(--mySet[i].end()) << endl;
}
return 0;
}