如何找到每一行的最大值和最小值
How can I find max and min value of every row
我的练习任务是如果最小值高于任何其他数组的最大值,则找到数组的索引。如果有多个,则只打印最低的索引。例如:
1 2 3 4 5
6 7 8 9 10
所以输出为 2,因为第二行的最小值高于另一行的最大值。但是我一直在搜索 每个 数组的最小值和最大值,所以我无法继续。
int numberOfTowns;
int numberOfDays;
cin >> numberOfTowns >> numberOfDays;
int temperature[100][100];
for (int i = 0; i < numberOfTowns; i++)
{
int maxValue = temperature[i][0];
int minValue = temperature[i][0];
for (int j = 0; j < numberOfDays; j++)
{
cin >> temperature[i][j];
if (temperature[i][j] > maxValue)
maxValue = temperature[i][j];
if (temperature[i][j] < minValue)
minValue = temperature[i][j];
}
cout << "Max: " << maxValue << endl;
cout << "Min: " << minValue << endl;
}
return 0;}
编辑:为了澄清起见,numberOfTowns 基本上表示行数,numberOfDays 表示列数。
我的输入和输出如下所示:
(3 是行数,5 是列数)
3 5
10 15 12 10 10
最大值:15
最小值:0
11 11 11 11 20
最多:20
最小值:0
18 16 16 16 20
最多:20
最小值:0
所以我的 Max 工作正常,但我的 Min 始终为 0。有人可以帮忙吗?
P.S.: 这是我的第一个问题,我对 C++ 有点陌生,如果我做错了什么,很抱歉。
在向该位置写入任何内容之前,您使用 temperature[i][0]
初始化 minValue
和 maxValue
。而是使用输入值来初始化它:
cin >> temperature[i][0];
int maxValue = temperature[i][0];
int minValue = temperature[i][0];
for (int j = 1; j < numberOfDays; j++) // start at 1
{
cin >> temperature[i][j];
if (temperature[i][j] > maxValue)
maxValue = temperature[i][j];
if (temperature[i][j] < minValue)
minValue = temperature[i][j];
}
minValue 从 0 开始。
应该从第一个值开始。
int numberOfTowns;
int numberOfDays;
cin >> numberOfTowns >> numberOfDays;
int temperature[100][100];
for (int i = 0; i < numberOfTowns; i++)
{
int maxValue;
int minValue;
for (int j = 0; j < numberOfDays; j++)
{
cin >> temperature[i][j];
if( j == 0 ) {
maxValue = temperature[i][0];
minValue = temperature[i][0];
continue;
}
if (temperature[i][j] > maxValue)
maxValue = temperature[i][j];
if (temperature[i][j] < minValue)
minValue = temperature[i][j];
}
cout << "Max: " << maxValue << endl;
cout << "Min: " << minValue << endl;
}
return 0;}
我的练习任务是如果最小值高于任何其他数组的最大值,则找到数组的索引。如果有多个,则只打印最低的索引。例如:
1 2 3 4 5
6 7 8 9 10
所以输出为 2,因为第二行的最小值高于另一行的最大值。但是我一直在搜索 每个 数组的最小值和最大值,所以我无法继续。
int numberOfTowns;
int numberOfDays;
cin >> numberOfTowns >> numberOfDays;
int temperature[100][100];
for (int i = 0; i < numberOfTowns; i++)
{
int maxValue = temperature[i][0];
int minValue = temperature[i][0];
for (int j = 0; j < numberOfDays; j++)
{
cin >> temperature[i][j];
if (temperature[i][j] > maxValue)
maxValue = temperature[i][j];
if (temperature[i][j] < minValue)
minValue = temperature[i][j];
}
cout << "Max: " << maxValue << endl;
cout << "Min: " << minValue << endl;
}
return 0;}
编辑:为了澄清起见,numberOfTowns 基本上表示行数,numberOfDays 表示列数。
我的输入和输出如下所示:
(3 是行数,5 是列数)
3 5
10 15 12 10 10
最大值:15 最小值:0
11 11 11 11 20
最多:20 最小值:0
18 16 16 16 20
最多:20 最小值:0
所以我的 Max 工作正常,但我的 Min 始终为 0。有人可以帮忙吗? P.S.: 这是我的第一个问题,我对 C++ 有点陌生,如果我做错了什么,很抱歉。
在向该位置写入任何内容之前,您使用 temperature[i][0]
初始化 minValue
和 maxValue
。而是使用输入值来初始化它:
cin >> temperature[i][0];
int maxValue = temperature[i][0];
int minValue = temperature[i][0];
for (int j = 1; j < numberOfDays; j++) // start at 1
{
cin >> temperature[i][j];
if (temperature[i][j] > maxValue)
maxValue = temperature[i][j];
if (temperature[i][j] < minValue)
minValue = temperature[i][j];
}
minValue 从 0 开始。 应该从第一个值开始。
int numberOfTowns;
int numberOfDays;
cin >> numberOfTowns >> numberOfDays;
int temperature[100][100];
for (int i = 0; i < numberOfTowns; i++)
{
int maxValue;
int minValue;
for (int j = 0; j < numberOfDays; j++)
{
cin >> temperature[i][j];
if( j == 0 ) {
maxValue = temperature[i][0];
minValue = temperature[i][0];
continue;
}
if (temperature[i][j] > maxValue)
maxValue = temperature[i][j];
if (temperature[i][j] < minValue)
minValue = temperature[i][j];
}
cout << "Max: " << maxValue << endl;
cout << "Min: " << minValue << endl;
}
return 0;}