今题想学学hash ~~~最后就搜出一道这题来
其实这道题很多方法可以解决 包括<iostream>中的sort
和map
做做这道题来理解理解hash 吧~~~
附上代码:
#include <iostream>
#include <cmath>
#include <stdio.h>
const int maxn=1100000;
const int N=500000;
using namespace std;
int hash[maxn];
int main()
{
int n,m;
int x;
while(cin>>n>>m)
{
for(int i=0;i<n;i++)
{
scanf("%d",&x);
hash[x+N]=1;
}
int num=maxn;
int cnt=0;
while(num--)
{
if(cnt==m)break;
if(hash[num])
{
cnt++;
if(cnt==1)printf("%d",num-N);
else printf(" %d",num-N);
}
}
cout<<endl;
}
return 0;
}