查找C语言中数组中的第二多 [英] C finding second largest number in an array

查看:101
本文介绍了查找C语言中数组中的第二多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我必须找到一个数组的第二大数目,如果该阵列中的每个数等于我应该写它的-∞。我写了这一点,任何人都可以检查,看看是否我也许可以优化它好一点?该数组只是一个例子,它应该是X [1 ... N],但因为我必须把它改写为伪code我把那当作一个范例

 的#include<&stdio.h中GT;
诠释的main()
{
    INT X [7] = {90,90,78,41,21,27,35};
    INT I,MAX,secmax,Y;
    secmax = 0;
    最大= X [0];
    对于(i = 1; I< = 7;我++)
        {
        如果(X [I]≥最大值)
            {
            secmax = MAX;
            最大= X [i]于;
            }
        否则如果(X [I]≥secmax&放大器;&放大器; X [1] - ;最大)
                {
            secmax = X [I];
            }
        }    对于(I = 0; I&下; 7;我+ +)
        如果(X [I] == X [I + 1])
            ÿ++;    如果(Y == 6)
        的printf(SEC最大零下nieskonczonosc \\ n);
    其他
        的printf(最大到%d的一个secmax到%d的\\ n,最大值,secmax);
    返回0;
}


解决方案

下面是你可以做什么:

  INT get_second_max(INT * X,为size_t N)
{
    INT最大值[2] = {-MAX_INT,-MAX_INT};
    INT I = 0,同样= 1;
    如果(NULL == ||点¯x0 == N)
        返回-MAX_INT;    最大值[0] = X [0];
    最大值[1] = X [0];
    对于(i = 1; I< N;我++){
        / *同样用于检查
         *如果数组包含
         *相同数量始终。
         * /
        相同&放大器; =(X [I] == X [I-1]);
        / *在第i次重复:
         * - >最大[0]存储的最大值
         * - >最大值[1]存储第二最大值
         * /        / *我们创下了新的最大值,我们必须:
         * 1,更新与当前最大值的第二最大值
         * 2.更新与新的最大值,我们发现目前的最高值
         * /
        如果(X [I]≥最大值[0]){
            最大[1] = MAX [0];
            最大值[0] = X [I];
        }否则如果(X [I]≥最大值[1]){
            最大值[1] = X [I];
        }
    }
    如果(相同){
        返回-MAX_INT;
    }其他{
        返回最大值[1];
    }
}

As the title says, I have to find the second largest number in an array and if every number in that array is equal I should write that it's -∞. I wrote this, could anyone check to see if I could perhaps optimize it a little better? This array is just an example, it should be x[1...n] but since I have to rewrite it to pseudocode I took that one as an example

#include <stdio.h>
int main()
{
    int x[7]={90,90,78,41,21,27,35};
    int i, max, secmax, y;
    secmax=0;
    max=x[0];
    for(i=1;i<=7;i++)
        {
        if (x[i]>max)
            {
            secmax=max;
            max=x[i];
            }
        else if (x[i]>secmax&&x[i]<max)
                {
            secmax=x[i];
            }
        }

    for(i=0;i<7;i++)
        if(x[i]==x[i+1])
            y++;

    if (y==6)
        printf("sec max to minus nieskonczonosc \n");
    else 
        printf("max to %d a secmax to %d\n",max,secmax);
    return 0;
}

解决方案

Here is what you can do :

int get_second_max(int *x, size_t n)
{
    int max[2] = {-MAX_INT,-MAX_INT};
    int i = 0, same = 1;
    if (NULL == x || 0 == n)
        return -MAX_INT;

    max[0] = x[0];
    max[1] = x[0];
    for(i = 1; i < n; i++) {
        /* same is used to check 
         * if the array contains 
         * the same number all along.
         */
        same &= (x[i] == x[i-1]);
        /* At the i-th iteration :
         * -> max[0] stores the maximum value
         * -> max[1] stores the second maximum value
         */

        /* We hit a new max value, we must :
         * 1. Update the second max value with the current max value
         * 2. Update the current max value with the new max we found
         */
        if(x[i] > max[0]) {
            max[1] = max[0];
            max[0] = x[i];
        } else if(x[i] > max[1]) {
            max[1] = x[i];
        }
    }
    if(same) {
        return -MAX_INT;
    } else {
        return max[1];
    }
}

这篇关于查找C语言中数组中的第二多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆