zoukankan      html  css  js  c++  java
  • 题目1174:查找第K小数-------qsort()可以直接对数组用;还有用unique(a,a+n);的,服!

    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;
    }
  • 相关阅读:
    Array
    java 设计模式
    Hashtable
    lettCode-Array
    最短路径 dijkstra
    算法:优先级队列
    7.29 DFS总结
    SZU:D89 The Settlers of Catan
    SZU:B47 Big Integer I
    7.25 RPN转换
  • 原文地址:https://www.cnblogs.com/jianrenguo/p/6544579.html
Copyright © 2011-2022 走看看