【问题标题】:How to find the positions of the max and min numbers in the array?如何找到数组中最大和最小数字的位置?
【发布时间】:2018-08-27 02:36:22
【问题描述】:
int A[5] = {2, 2, 1, 4, 1};    
int max = A[0], min = A[0];
int k,l;

for (i = 0; i < 5; i++) {
    if (A[i] >= max) {
        max = A[i];
        k = i;
    }
}

for (j = 0; j < 5; j++) {
    if (A[j] <= min) {
        min = A[j];
        l = j;
    }
}

printf("densely is: %d ", k);
printf("\n");
printf("loosely is: %d ", l);

程序将densely (max) 的位置打印为3。 它将loosely (min) 的位置打印为4。 但是looselymin)位置的正确答案应该是24。 在我的例子中,max 的数字是4,所以位置应该是3(从0 开始计算)。 min 编号为1,因此位置应为24。 现在,我的代码只显示min 号码的一个位置:4。 它应该打印两个位置:24

我想打印出maxmin 数字的所有位置。

【问题讨论】:

  • 你为什么这么认为?该代码正在寻找最后一次出现的最大值和最小值。最大值为 4,最小值为 1。最后一次出现在索引 4(min) 和索引 3(max)。
  • 您希望第一次出现吗?然后尝试&gt; 代替&gt;=&lt; 代替&lt;=
  • 我想打印出最大值和最小值的所有位置。在这种情况下,最大数量为 4,因此位置应为 3(从 0 开始计数)。最小数字是 1,所以位置应该是 2 和 4。现在我的代码只显示最小数字位置是 4。我想要的是它显示 2 和 4
  • 这确实有助于理解您的问题。我建议在你的问题中解释这一点。使用edit 来做到这一点。这也有助于避免“松散”和“densley”这两个词引起的混淆。要么避免这些词,要么解释它们。
  • 基本上,您可能希望输出多个关于最大索引的信息和多个关于最小索引的信息。您是否考虑过使用多个变量来存储这些变量?您可能需要在这两种情况下使用与数组中的成员一样多的变量,因为所有数组成员都可以包含最大值和最小值。

标签: c arrays position max min


【解决方案1】:

这实际上是一个很好的问题。解决方案的关键是在数组A 中找到minmax 的值。那部分已经完成并且运行良好。这里唯一的改进是从索引1 开始,并从&gt;=&lt;= 比较中删除=
缺少的部分是记住minmax 的所有索引。

这很难一次性做到。您可以一次性找到minmax

一旦在第二遍中获得minmax,您就可以在名为maximumsminimums 的附加索引表中标记maxmin 的所有索引位置。 (因为我想打印这些值,所以我有两个单独的循环。)

解决方法很简单:

#include <stdio.h>

// Find all minimums and all maximums in the array A
// Find and remember where `max` and `min` values were in the array A
// The algorithm is symmetrical for minimum and maximum 

int main(void)
{             // 0  1  2  3  4 
    int A[5] = { 2, 2, 1, 4, 1};    

    int maximums[5] = {0};  // we keep max positions here
    int minimums[5] = {0};  // we keep min positions here

    int max = A[0];  // we assume max at index 0
    int min = A[0];  // we assume min at index 0

    for (int i = 1; i < 5; i++) {       // we can start from index 1 because of the above assumption
        if (A[i] > max) max = A[i];     // densely 
        if (A[i] < min) min = A[i];     // loosely
    }

    // Find the all elements equal to `max` and `min` 
    // and register them in `maximums` and `minimums` arrays

    // Print the results:
    printf("max value is: %d and it occurs at the following index(es):\n", max);         
    for (int i = 0; i < 5; i++) {

        if (A[i] == max){ 
            maximums[i] = 1;   // mark this index as maximum being here
            printf("%d ", i);
        }
    }

    printf("\nmin value is: %d and it occurs at the following index(es):\n", min );   
    for (int i = 0; i < 5; i++) {

        if (A[i] == min){ 
            minimums[i] = 1;   // mark this index as minimum being here
            printf("%d ", i);
        }    
    }

    // At this moment we not only printed the results 
    // but the array minimums and maximums remember where the `min` and `max` were.

    return 0;
}

输出:

max value is: 4 and it occurs at the following index(es):                                                                                       
3                                                                                                                                               
min value is: 1 and it occurs at the following index(es):                                                                                       
2 4

【讨论】:

  • 谢谢。这真的很有帮助
猜你喜欢
  • 2011-10-31
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 2011-04-17
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多