Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ЗАДАЧА №3
- *
- * Найти в массиве целых чисел три минимальных числа. Числа могут повторяться.
- * Для массива [2, 1, 0, 1] ответ будет [0, 1, 1].
- *
- * ПИНФ-11, Нефедов Д. В. — <eanmos@ya.ru>.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #define ARRLEN(a) (sizeof(a) / sizeof(a[0]))
- #define SWAPINT(a, b) do { const int tmp = (*a); (*a) = (*b); (*b) = tmp; } while (0)
- static void sortThreeElementsArray(int a[3])
- {
- if (a[0] > a[2])
- SWAPINT(&a[0], &a[2]);
- if (a[1] > a[2])
- SWAPINT(&a[1], &a[2]);
- if (a[0] > a[1])
- SWAPINT(&a[0], &a[1]);
- }
- static int *getThreeLowestValues(const int a[], size_t n)
- {
- static int ret[3];
- for (size_t i = 0; i < 3; i++)
- ret[i] = INT_MAX;
- for (size_t i = 0; i < n; i++) {
- if (a[i] < ret[2]) {
- ret[2] = a[i];
- sortThreeElementsArray(ret);
- }
- }
- return ret;
- }
- int main(void)
- {
- const int array[] = {-4, 20, 1, 0, 9, 4, 12, 4, 7, 3};
- const int *answer = getThreeLowestValues(array, ARRLEN(array));
- for (size_t i = 0; i < 3; i++)
- printf("%d ", answer[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement