Title Link: http://ac.jobdu.com/problem.php?pid=1034
-
Title Description:
-
Zhejiang Tongxiang Wu Town A total of n individuals, please find the town of the former m monopoly.
-
Input:
-
The input contains multiple sets of test cases.
Each use case first contains 2 integers n (0<n<=100000) and M (0<m<=10), Where: N is the number of people in town, M is the number of millionaires that need to be found, and the next line is to enter the wealth value of N people in the town.
N and m at 0 indicate the end of the input.
-
Output:
-
please output wuzhen former M monopoly property number, property of the row front, if the monopoly less than m, then all output, each group of output accounted for a row.
-
Sample input:
-
3 12 5-15 31 2 3 4 50 0
-
Sample output:
-
55 4 3
Source: 2009 Zhejiang University computer and software engineering research on the real problem of life test
This topic is relatively simple, thinking:
If N<=m then enter n number into the array, then sort in descending order, then direct output;
If n>m, first enter the first m number into the array. Then, when you enter a number of temp each time, determine if temp is larger than the smallest number min in the array, and if it overrides the smallest digit min in the array, skip, enter the next, and then continue ...
After the input and processing is complete, sort in descending order and then output.
The code is as follows:
1#include <stdio.h>2#include <stdlib.h>3 intcmpConst void*a,Const void*b)4 {5 return*(int*) B-* (int*) A;6 }7 intFinminindex (intA[],intLen);//returns the subscript for the smallest element of a array. A the number of array elements is Len8 intMain ()9 {Ten intn,m; One inta[ One]={0}; A inti; - intMini; - inttemp; the - while(SCANF ("%d%d", &n,&m)! =EOF) - { - if(n==0&&m==0) Break; + if(n<=m) - { + for(i=0; i<n;i++) scanf ("%d",&a[i]); AQsort (A,n,sizeof(a[0]), CMP); atprintf"%d", a[0]); - for(i=1; i<n;i++) printf ("%d", A[i]); -printf"\ n"); - } - Else - { in for(i=0; i<m;i++) - { toscanf"%d",&a[i]); + } - for(i=m;i<n;i++) the { *scanf"%d",&temp); $Mini=Finminindex (a,m);Panax Notoginseng if(Temp>a[mini]) a[mini]=temp; - } theQsort (A,m,sizeof(a[0]), CMP); +printf"%d", a[0]); A for(i=1; i<m;i++) printf ("%d", A[i]); theprintf"\ n"); + } - } $ return 0; $ } - intFinminindex (intA[],intLen//returns the subscript for the smallest element of a array. A the number of array elements is Len - { the inti,minindex=0; - for(i=1; i<len;i++)Wuyi { the if(A[i]<a[minindex]) minindex=i; - } Wu returnMinindex; -}
Nine degrees OJ topic 1034: Looking for a Millionaire