1 条题解

  • 0
    @ 2021-6-14 23:20:22

    C :

    #include<stdio.h>
    int main(){
        int amount,k,i;
        scanf("%d%d",&amount,&k);
        int a[amount],left[amount],right[amount];
        for(i=0;i<amount;i++)
        scanf("%d",&a[i]);
        printf("%d",search(a,k,amount));
    }
    int search(int b[],int kk,int amountb){
    int left[amountb],right[amountb];
    int i,numl=0;
    int numr=0;
    for (i=1;i<amountb;i++){
    if(b[i]<=b[0]){left[numl]=b[i];numl++;}
    if(b[i]>b[0]){right[numr]=b[i];numr++;}
    }
    if(numr>(kk-1))return search(right,kk,numr);
    if(numr<(kk-1))return search(left,kk-numr-1,numl);
    else return b[0];
    }
    
    

    C++ :

    #include<iostream>
    #include<cstring>
    #include<string>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    
    int main(){
    	int n,k,t=1;cin>>n>>k;
    	int a[n+1],ans[n+1];
    	for(int i=1;i<=n;i++)
    	  cin>>a[i];
    	sort(a+1,a+n+1);
    	
    	ans[1]=a[1];
    	for(int i=2;i<=n;i++)
    	  if(a[i]!=ans[t]){
    	  	ans[t+1]=a[i];
    	  	t++;
    	  }
    	if(k>t) {cout<<"NO RESULT";return 0;}
    	else cout<<ans[n-k+1]; 
    	return 0;
    }
    

    Pascal :

    program k1;
    var
    a:array[1..1000000] of longint;
    k,n,i:longint;
    procedure sort(l,r: longint);
    var
    i,j,x,y: longint;
    begin
     i:=l;j:=r;x:=a[(l+r) div 2];
     repeat
     while a[i]<x do inc(i);
     while x<a[j] do dec(j);
     if not(i>j) then
     begin
      y:=a[i];a[i]:=a[j];a[j]:=y;
      inc(i);j:=j-1;
     end;
     until i>j;
     if l<j then sort(l,j);
     if i<r then sort(i,r);
    end;
    begin
     readln(n,k);
     for i:=1 to n do
     read(a[i]);readln;
     sort(1,n);
     writeln(a[n-k+1]);
    end.
    

    Java :

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		int n=input.nextInt();
    		int k=input.nextInt();
    		int[] a=new int[n];
    		for(int i=0;i<n;i++) {
    			a[i]=input.nextInt();
    		}
    		Arrays.sort(a);
    		int b=0;
    		for(int i=0;i<n;i++) {
    			b=(a[n-k]);
    		}
    		System.out.println(b);
    	}
    }
    
    • 1

    信息

    ID
    135
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者