diff options
Diffstat (limited to 'inf/rtkš/2.c')
-rw-r--r-- | inf/rtkš/2.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/inf/rtkš/2.c b/inf/rtkš/2.c new file mode 100644 index 0000000..63f0953 --- /dev/null +++ b/inf/rtkš/2.c @@ -0,0 +1,58 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#define MIN(a,b) ((a) > (b) ? (b) : (a)) +#define MAX(a,b) ((a) < (b) ? (b) : (a)) +struct stolpec { + int položaj; + int višina; + int izmeril; +}; +int primerjaj_stolpca (const void * a, const void * b) { + const struct stolpec * c = (const struct stolpec *) a; + const struct stolpec * d = (const struct stolpec *) b; + return d->višina - c->višina; +} +void natisni (int n, const struct stolpec * s) { + for (int i = 0; i < n; i++) { + printf("%d: ", s[i].položaj); + for (int j = 0; j < s[i].višina; j++) + printf("@"); + printf("\n"); + } +} +int voda (int n, struct stolpec * s) { + struct stolpec razv[n]; + memcpy(razv, s, n*sizeof *s); + qsort(razv, n, sizeof *s, primerjaj_stolpca); + natisni(n, s); + printf("---------\n"); + natisni(n, razv); + int povr = 0; + for (int i = 0; i < n-1; i++) { + int max_pol = MAX(razv[i].položaj, razv[i+1].položaj); + int min_pol = MIN(razv[i].položaj, razv[i+1].položaj); + if (max_pol - min_pol > 1) { + printf("med stoplcema z indeksoma %d in %d\n", min_pol, max_pol); + for (int j = min_pol+1; j < max_pol; j++) { + if (s[j].izmeril) + continue; + int tapovr = razv[i+1].višina - s[j].višina; + if (tapovr > 0) + povr += tapovr; + s[j].izmeril++; + printf("izmeril stoplec indeks %d - povr je %d ... min_viš je %d, s[j].višina je %d\n", j, povr, razv[i+1].višina, s[j].višina); + } + } + } + return povr; +} +int main (int argc, char ** argv) { + int n = argc-1; + struct stolpec * stolpci = calloc(n, sizeof *stolpci); + for (int i = 0; i < n; i++) { + stolpci[i].položaj = i; + stolpci[i].višina = atoi(argv[i+1]); + } + return voda(n, stolpci); +} |