mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add single-number/c/function
This commit is contained in:
parent
c49f8e1c61
commit
25c07b1858
@ -6,13 +6,13 @@ bool is_odd(int number) {
|
|||||||
return number % 2 == 1;
|
return number % 2 == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int find_outlier_number(int* numbers, size_t length) {
|
int find_outlier_number(int* numbers, size_t numbers_size) {
|
||||||
int outlier_number;
|
int outlier_number;
|
||||||
int* odd_numbers = malloc(length * sizeof(int));
|
int* odd_numbers = malloc(numbers_size * sizeof(int));
|
||||||
int* even_numbers = malloc(length * sizeof(int));
|
int* even_numbers = malloc(numbers_size * sizeof(int));
|
||||||
size_t odd_length = 0;
|
size_t odd_length = 0;
|
||||||
size_t even_length = 0;
|
size_t even_length = 0;
|
||||||
for (size_t index = 0; index < length; index++) {
|
for (size_t index = 0; index < numbers_size; index++) {
|
||||||
if (is_odd(numbers[index])) {
|
if (is_odd(numbers[index])) {
|
||||||
odd_numbers[odd_length] = numbers[index];
|
odd_numbers[odd_length] = numbers[index];
|
||||||
odd_length++;
|
odd_length++;
|
||||||
@ -32,16 +32,16 @@ int find_outlier_number(int* numbers, size_t length) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
size_t length = 0;
|
size_t numbers_size = 0;
|
||||||
int number;
|
int number;
|
||||||
int* numbers = malloc(sizeof(int));
|
int* numbers = malloc(sizeof(int));
|
||||||
do {
|
do {
|
||||||
scanf("%d", &number);
|
scanf("%d", &number);
|
||||||
numbers = realloc(numbers, sizeof(int) * (length + 1));
|
numbers = realloc(numbers, sizeof(int) * (numbers_size + 1));
|
||||||
numbers[length] = number;
|
numbers[numbers_size] = number;
|
||||||
length++;
|
numbers_size++;
|
||||||
} while (fgetc(stdin) != EOF);
|
} while (fgetc(stdin) != EOF);
|
||||||
printf("%d\n", find_outlier_number(numbers, length));
|
printf("%d\n", find_outlier_number(numbers, numbers_size));
|
||||||
free(numbers);
|
free(numbers);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
3
challenges/single-number/solutions/c/function/README.md
Normal file
3
challenges/single-number/solutions/c/function/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# single-number/c/function
|
||||||
|
|
||||||
|
Created by [@theoludwig](https://github.com/theoludwig) on 21 August 2023.
|
25
challenges/single-number/solutions/c/function/solution.c
Normal file
25
challenges/single-number/solutions/c/function/solution.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int single_number(int* numbers, size_t numbers_size) {
|
||||||
|
int result = numbers[0];
|
||||||
|
for (size_t index = 1; index < numbers_size; index++) {
|
||||||
|
result = result ^ numbers[index];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
size_t numbers_size = 0;
|
||||||
|
int number;
|
||||||
|
int* numbers = malloc(sizeof(int));
|
||||||
|
do {
|
||||||
|
scanf("%d", &number);
|
||||||
|
numbers = realloc(numbers, sizeof(int) * (numbers_size + 1));
|
||||||
|
numbers[numbers_size] = number;
|
||||||
|
numbers_size++;
|
||||||
|
} while (fgetc(stdin) != EOF);
|
||||||
|
printf("%d\n", single_number(numbers, numbers_size));
|
||||||
|
free(numbers);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user