【问题标题】:Third Largest element in array数组中的第三大元素
【发布时间】:2019-07-14 06:55:17
【问题描述】:

有 6 个测试用例,其中 5 个是正确的。 程序即将找到第三大元素 在一个数组中。你能找到最后一个错误吗 测试用例能通过吗?

#include<stdio.h>
void thirdLargest(int arr[],int arr_size);
int main()
{
    int n,a[1000],i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    thirdLargest(a,n);
    return 0;
}
void thirdLargest(int arr[],int arr_size)
{
    int i,j,temp;
    for(i=0;i<arr_size-1;i++)
    {
        for(j=0;j<arr_size-i-1;j++)
        {
            if(arr[j]>arr[j+1])
            {
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;
            }
        }
    }
    printf("The third Largest element is %d",arr[arr_size-3]);
}

【问题讨论】:

  • 你没有描述测试用例,所以我假设它是一个秘密测试的在线法官。请不要指望我们会为您赢得挑战。
  • 你不需要对它进行排序,只需遍历数组并不断更新最大的 3 个元素。
  • 当元素超过 1000 个时会发生什么?
  • @Quimby 叹息....
  • 根据meta.stackoverflow.com/questions/334822/… 此处描述的折衷方案,我确实提供了帮助(尽管是在评论中而不是在答案中)。到现在为止,至少有两个人含蓄地证明他们发现我的暗示是正确的......如果你做出妥协,我将升级为答案并继续提供更多提示。

标签: c


【解决方案1】:

为了好玩,我编写了一个似乎可以解决您的问题以及第 4、第 5 级等问题的代码;此外,我还添加了在运行时计算随机数和/或管理级别值的功能。

您可能会获得其他情况,而不是操纵 2 个预处理器宏:

  • #define RAND_VALUE 0 0 需要手动插入数字,1 计算随机数。

  • #define LEVEL 3 3 用于管理你的情况,0 以外的其他值可以管理其他固定情况(第 4、5、...、第 n 级),0 用于管理动态分配,函数接收参数level

此代码不会验证插入的数字是否小于值级别。这可能是个问题,应该以某种方式加以纠正。

另一种错误情况可能是插入了 1000 多个数字。这种情况也应该以某种方式纠正。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
#include <limits.h>

#define LEVEL 3
#define RAND_VALUE 0

#ifndef LEVEL
int levelLargest(int * a, int len, unsigned int level);
#else
int levelLargest(int * a, int len);
#endif

#ifndef LEVEL
int levelLargest(int * a, int len, unsigned int level)
#else
int levelLargest(int * a, int len)
#endif
{
#if LEVEL == 0
    int i,j,k,* max;
    int l=(int)level;
    max=malloc(level*sizeof(int));
#else
    int i,j,k,max[LEVEL];
    const int l=LEVEL;
#endif

    for(i=0;i<l;i++)
        max[i]=INT_MIN;

    for(i=0;i<len;i++) {
        for(j=0;j<l;j++) {
            if (a[i]>max[j]) {
                for(k=l-1;k>j;k--) {
                    max[k]=max[k-1];
                }
                max[j]=a[i];
                break;
            }
        }
    }

    i=max[l-1];
#if LEVEL == 0
    free(max);
#endif

    return i;
}

int main()
{
    int n,a[1000],i;

#if RAND_VALUE != 0
    srand((unsigned)time(NULL));
#endif

    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
#if RAND_VALUE != 0
        /* Computes numbers between +100 and -100 */
        a[i]=rand() % 200 - 100;
        printf("%+4d ",a[i]);
#else
        scanf("%d",&a[i]);
#endif
    }

#if RAND_VALUE != 0
    puts("");
#endif

    printf("The 3rd largest is: %d\n",
#if LEVEL == 0
           levelLargest(a,n,3)
#else
           levelLargest(a,n)
#endif
    );

    return 0;
}

P.S.:对于大级别(级别 x)值,使用 qsort 对值进行降序排序,然后指示排序数组的第 x 个元素,可能会更好。

【讨论】:

    【解决方案2】:

    解决方案 1:使用散列

    private static void ThirdLargestElement()
    {
        int[] arr = { 2, 4, 1, 3, 5 };
        int third = GetThirdLargestElement(arr);
        Console.WriteLine("Third element :{0}", third);
    }
    
    private static int GetThirdLargestElement(int[] arr)
    {
        HashSet<int> track = new HashSet<int>();
        int size = arr.Length;
    
        for (int i = 0; i < size; i++)
        {
            track.Add(arr[i]);
            if (track.Count >= 3)
                track.Remove(track.Min());
        }
    
        if (track.Count == 3)
            return track.Min();
    
        return track.Min();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多