C:(100分)
在原来P1177的基础上修改了一下
#define _CRT_SECURE_NO_WARNINGS 1
#inclass="superseo">clude
int arr[5000005] = { 0 };
void myswap(int i, int j)
{
int a;
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
void mysort(int x, int y, int a)
{
int i = x;
int j = y;
int zhong = (x + y) / 2;
int k = arr[zhong];
while (i <= j)
{
while (arr[j] > k)
{
j--;
}
while (arr[i] < k)
{
i++;
}
if (i <= j)
{
myswap(i, j);
i++;
j--;
}
}
if (a <= j)
{
mysort(x, j, a);
}
else if (a >= i)
{
mysort(i, y, a);
}
else
{
return;
}
return;
}
int main()
{
int n = 0;
int i = 1;
int k = 0;
scanf("%d %d", &n, &k);
while (scanf("%d", &arr[i]) != EOF && getchar() != '\n')
{
i++;
}
mysort(1, n, k + 1);
printf("%d", arr[k + 1]);
return 0;
}
C++(100分)
直接用sort或者nth_element直接搞定了,但题目不给用
C++试过在原来C的基础上修改的输入和输出,然后居然超时过不了
然后看了看题解要搞快读,就去学习了一下
#define _CRT_SECURE_NO_WARNINGS 1
#include
using namespace std;
int arr[5000005] = { 0 };
inline int myread()
{
int num = 0; //数值
int fu = 1; //表示符号,1为证,-1为负
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
{
fu *= -1;
}
ch = getchar();
}
while ('0' <= ch && ch <= '9')
{
num = num * 10 + (ch - '0');
ch = getchar();
}
return fu * num;
}
void myswap(int i, int j)
{
int a;
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
void mysort(int x, int y, int a)
{
int i = x;
int j = y;
int zhong = (x + y) / 2;
int k = arr[zhong];
while (i <= j)
{
while (arr[j] > k)
{
j--;
}
while (arr[i] < k)
{
i++;
}
if (i <= j)
{
myswap(i, j);
i++;
j--;
}
}
if (a <= j)
{
mysort(x, j, a);
}
else if (a >= i)
{
mysort(i, y, a);
}
else
{
return;
}
return;
}
int main()
{
int n = 0;
int k = 0;
n = myread();
k = myread();
for (int i = 1; i <= n; i++)
{
arr[i] = myread();
}
mysort(1, n, k + 1);
printf("%d", arr[k + 1]);
return 0;
}
python:(100分)
本想把C的代码的python化
但无论怎么搞都会莫名RE
于是就只好用list.sort
但同样会RE甚至MLE
求助
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)