AC:
#include<stdio.h> #include<stdlib.h> #include<cstring> int a[1001]; int cmp(void const *a,void const *b); int main() { int n; while(scanf("%d",&n)!=EOF) { int i,j,k; for(i=0;i<n;i++) scanf("%d",&a[i]); qsort(a,n,sizeof(int),cmp); scanf("%d",&k); for(i=0;i<n;i++) { if (a[i]!=a[i+1]) k--;//注意找相同元素的策略; if (k==0) break; } printf("%d ",a[i]); } return 0; } int cmp(void const *a,void const *b) { return *(int*)a-*(int*)b; }
AC:
#include<iostream> #include<algorithm> using namespace std; int a[1001]; int main() { int n,k; while(cin>>n){ for(int i=0;i<n;++i) cin>>a[i]; sort(a,a+n);//默认是升序 unique(a,a+n);//去掉重复的元素,只剩一个 cin>>k; cout<<a[k-1]<<endl; } return 0; }