http://acm.hdu.edu.cn/showproblem.php?pid=2523

求差的绝对值第k大的数,注意相同的差算一个。

分析出差的绝对值在[0,2000]这个范围内,由此可以搞出hash数组

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int abs(int a)
{
    return a>0?a:-a;
}

int a[3000],hash[5000];
int main()
{
    int t,n,k;
    int i,j; 
    scanf("%d",&t);
    while(t--)
    {
        memset(hash,0,sizeof(hash));
        scanf("%d%d",&n,&k);
        for(i=0;i<n;i++)
            scanf("%d",a+i);
        for(i=0;i<n-1;i++)
            for(j=i+1;j<n;j++)
                hash[abs(a[i]-a[j])]=1;
        int cnt=0;
        for(i=0;i<=2000;i++)
            if(hash[i]){
                cnt++;
                if(cnt==k)
                    break;
            }
        printf("%d\n",i);
    }
    return 0;
}

 

相关文章:

  • 2021-06-08
  • 2021-10-06
  • 2021-10-24
  • 2021-07-12
  • 2022-02-17
  • 2021-10-21
  • 2022-01-04
猜你喜欢
  • 2021-12-22
  • 2021-11-18
  • 2022-01-20
  • 2022-12-23
  • 2021-08-25
  • 2022-12-23
  • 2021-11-22
相关资源
相似解决方案