mp2i-info/dm3/dm3.c
2025-10-21 16:34:47 +02:00

77 lines
1.5 KiB
C

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int spitze(const bool t[], int size) {
for (int i = 0; i < size; i += 1) {
if (t[0] ^ t[i]) {
return i + 1;
}
}
return size;
}
bool nul(const bool t[], int size) {
while (size > 0 && !t[size - 1]) {
size -= 1;
}
int count = 0;
for (int i = 0; i < size; i += 1) {
count += t[i];
if (count <= i / 2) {
return false;
}
}
return true;
}
double modfloat(double a, double b) {
int q = (int)(a / b);
return a - (double)(b * q);
}
void minmax(const int t[], int size, int *min, int *max) {
if (size) {
*min = t[0];
*max = t[0];
}
for (int i = 1; i < size; i += 1) {
if (t[i] < *min) {
*min = t[i];
} else if (t[i] > *max) {
*max = t[i];
}
}
}
int medianemax(const int t[], int size) {
if (!size) {
return 0;
}
bool forward = true;
int min = 0;
int max = size - 1;
while (min < max) {
if (forward) {
min += 1;
if (t[min] >= t[max]) {
forward = false;
}
} else {
max -= 1;
if (t[max] >= t[min]) {
forward = true;
}
}
}
return min;
}
void derive(double t[], int size) {
for (int i = 1; i < size; i += 1) {
t[i - 1] = i * t[i];
}
t[size - 1] = 0;
}