Created
December 24, 2015 18:43
-
-
Save vslaykovsky-zz/d7e1c757e6824e42b411 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char* argv[]) { | |
int* vals; | |
size_t n; | |
size_t m; | |
int i; | |
if (scanf("%llu", &n) != 1) { | |
fprintf(stderr, "Invalid n\n"); | |
exit(1); | |
} | |
if (!(vals = malloc(n * sizeof(*vals)))) { | |
fprintf(stderr, "malloc failed\n"); | |
exit(1); | |
}; | |
for (i = 0; i < n; ++i) { | |
if (scanf("%d", &vals[i]) != 1) { | |
fprintf(stderr, "scanf failed"); | |
exit(1); | |
} | |
} | |
if (scanf("%llu", &m) != 1) { | |
fprintf(stderr, "Invalid M\n"); | |
exit(1); | |
} | |
if (m > n) { | |
fprintf(stderr, "M > N\n"); | |
exit(1); | |
} | |
for (i = 0; i < n; ++i) { | |
int k; | |
for (k = 0; k < i; ++k) { | |
if (vals[k] < vals[k+1]) { | |
int buf; | |
buf = vals[k]; | |
vals[k] = vals[k + 1]; | |
vals[k + 1] = buf; | |
} | |
} | |
} | |
for (i = 0; i < m; ++i) { | |
printf("%d\n", vals[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment