P1923 求第k小的数

题目链接:P1923

Describe:
输入 nnn(n<5000000n<5000000n<5000000 且 nnn 为奇数) 个数字 ai(0<ai<109)a_i(0<a_i<10^9)ai(0<ai<109) ,输出这些数字的第 kkk 小的数。最小的数是第 0 小。
Input:
Output:
Sample Input:
5 1
4 3 2 1 5
Sample Output:
2

解题思路:

快读+sort排序,不用快读会TLE

AC代码:

 

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 inline int read() // 读入整数
 6 {
 7     int x = 0, y = 1; char c = getchar(); // y标记正负号
 8     while(c < '0' || c > '9') {if(c == '-') y = -1; c = getchar();}
 9     while(c >= '0' && c <= '9') x = x*10 + c - '0', c = getchar();
10     return x*y;
11 }
12 int main()
13 {
14     int n,k;
15     long a[5000000];
16     n = read();
17     k = read();
18     for(int i = 0; i < n; i++) a[i] = read();
19     sort(a,a+n);
20     printf("%ld",a[k]);
21     return 0;
22 }

 更新快读:

可读浮点数,整数

示例代码:

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 inline double read()
 5 {
 6     int f = 0, y = 1; double x = 0, d = 0, s = 0.1; char c = getchar();
 7     while(c < '0' || c > '9') {if(c == '-') y = -1; c = getchar();}
 8     while((c >= '0' && c <= '9') || c == '.')
 9     {
10         if(c == '.') {f = 1; c = getchar(); continue;}
11         if(f) d += (c-'0')*s, s *= 0.1;
12         else x = x*10+c-'0';
13         c = getchar();
14     }
15     return (x+d)*y;
16 }
17 int main()
18 {
19     double d;
20     int n;
21     n = read();
22     d = read();
23     cout << n << " " << d;
24     return 0;
25 }

 

posted @ 2020-09-04 09:38  不敢说的梦  阅读(296)  评论(0)    收藏  举报