-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/witek-formanski/WDP
- Loading branch information
Showing
24 changed files
with
372 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int less_int(const void *a, const void *b) | ||
{ | ||
int _a = *(int *)a; | ||
int _b = *(int *)b; | ||
if (_a < _b) | ||
return -1; | ||
else if (_a == _b) | ||
return 0; | ||
else | ||
return 1; | ||
} | ||
|
||
int main() | ||
{ | ||
int maxN = 1e3; // maksymalna wartość jaką może przyjąć n | ||
int maxDist = 1e7; // maksymalna wartość dystansu hotelu od startu | ||
|
||
unsigned int seed; | ||
scanf("%u", &seed); | ||
srand(seed); | ||
|
||
int n = (rand() % maxN) + 1; | ||
printf("%d\n", n); | ||
|
||
int *t = (int *)malloc((unsigned)n * sizeof(int)); | ||
for (int i = 0; i < n; ++i) | ||
t[i] = rand() % maxDist; | ||
qsort(t, (unsigned)n, sizeof(int), less_int); | ||
|
||
for (int i = 0; i < n; i++) | ||
printf("%d %d\n", (rand() % n) + 1, t[i]); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/bash | ||
|
||
opcje="-std=c17 -pedantic -Wall -Wextra -Wformat-security -Wduplicated-cond -Wfloat-equal -Wshadow -Wconversion -Wjump-misses-init -Wlogical-not-parentheses -Wnull-dereference -Wvla -Werror -fstack-protector-strong -fsanitize=undefined -fno-sanitize-recover -g -fno-omit-frame-pointer -O1" | ||
|
||
gcc $opcje lab05.c -o wzo #zastąp lab05 nazwą wzorcówki | ||
gcc $opcje brut05.c -o brut #zastąp brut05 nazwą bruta | ||
gcc $opcje gen05.c -o gen #zastąp gen05 nazwą generatorki | ||
|
||
test_nr=1000 #ustaw ile testów chcesz wykonać | ||
|
||
for(( i=1; i<=$test_nr; i++)) do | ||
echo $i | ./gen > ./wejscie.in | ||
./brut < ./wejscie.in > brut.out | ||
./wzo < ./wejscie.in > wzo.out | ||
|
||
if diff -qb brut.out wzo.out; | ||
then | ||
echo "test $i OK"; | ||
else | ||
echo "test $i ZLE"; | ||
break; | ||
fi | ||
done |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
//Uses https://en.wikipedia.org/wiki/Flexible_array_member at end of struct | ||
typedef struct { | ||
int dl; | ||
double el[]; | ||
} ciag; | ||
|
||
ciag *roznicowy(ciag *c) { | ||
ciag *r = malloc(sizeof(ciag) + (size_t)(c->dl - 1) * sizeof(double)); | ||
r->dl = c->dl - 1; | ||
|
||
for (int i = 0; i < r->dl; i++) | ||
r->el[i] = c->el[i + 1] - c->el[i]; | ||
|
||
return r; | ||
} | ||
|
||
ciag *readCiag() { | ||
int dl; | ||
if(!scanf("%d", &dl)) printf("wrong input"); | ||
|
||
ciag *c = malloc(sizeof(ciag) + (size_t)dl * sizeof(double)); | ||
c->dl = dl; | ||
|
||
for (int i = 0; i < dl; i++) | ||
if(!scanf("%lf", &(c->el[i]))) printf("wrong input"); | ||
|
||
return c; | ||
} | ||
|
||
void printCiag(ciag *c) { | ||
for (int i = 0; i < c->dl; i++) | ||
printf("%lf ", c->el[i]); | ||
} | ||
|
||
int main() { | ||
ciag *c = readCiag(); | ||
|
||
printCiag(roznicowy(c)); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
//Uses https://en.wikipedia.org/wiki/Flexible_array_member at end of struct | ||
typedef struct ciag { | ||
int dl; | ||
struct ciag *next; | ||
double el[]; | ||
} ciag; | ||
|
||
ciag *roznicowyRec(ciag *c) { | ||
c->next = malloc(sizeof(ciag) + (size_t)(c->dl - 1) * sizeof(double)); | ||
c->next->dl = c->dl - 1; | ||
c->next->next = NULL; | ||
|
||
for (int i = 0; i < c->next->dl; i++) | ||
c->next->el[i] = c->el[i + 1] - c->el[i]; | ||
|
||
if(c->next->dl != 0) roznicowyRec(c->next); | ||
|
||
return c; | ||
} | ||
|
||
ciag *readCiag() { | ||
int dl; | ||
if(!scanf("%d", &dl)) printf("wrong input"); | ||
|
||
ciag *c = malloc(sizeof(ciag) + (size_t)dl * sizeof(double)); | ||
c->dl = dl; | ||
c->next = NULL; | ||
|
||
for (int i = 0; i < dl; i++) | ||
if(!scanf("%lf", &(c->el[i]))) printf("wrong input"); | ||
|
||
return c; | ||
} | ||
|
||
void printCiag(ciag *c) { | ||
for (int i = 0; i < c->dl; i++) | ||
printf("%lf ", c->el[i]); | ||
|
||
printf("\n"); | ||
|
||
if(c->next != NULL) printCiag(c->next); | ||
} | ||
|
||
int main() { | ||
ciag *c = readCiag(); | ||
|
||
printCiag(roznicowyRec(c)); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
int **obliczSumPref(int **t, int size_y, int size_x){ | ||
int **sumPref = (int**)malloc((unsigned int)(size_y + 1) * sizeof(int*)); | ||
for(int i = 0; i < size_y + 1; i++) | ||
sumPref[i] = (int*)malloc((unsigned int)(size_x + 1) * sizeof(int)); | ||
for(int i = 0; i < size_y + 1; i++) | ||
sumPref[i][0] = 0; | ||
for(int i = 0; i < size_x + 1; i++) | ||
sumPref[0][i] = 0; | ||
for(int y = 0; y < size_y; y++) | ||
for(int x = 0; x < size_x; x++) | ||
sumPref[y + 1][x + 1] = t[y][x] + sumPref[y][x + 1] + sumPref[y + 1][x] - sumPref[y][x]; | ||
return sumPref; | ||
} | ||
|
||
int prostakat(int **a, int size_y, int size_x, int k){ | ||
int minObw = 2 * (size_y + 1) + 2 * (size_x + 1); | ||
int x1, x2, drzewa, obwod; | ||
int **sumPref = obliczSumPref(a, size_y, size_x); | ||
|
||
for(int y1 = 0; y1 < size_y; y1++){ | ||
for(int y2 = y1; y2 < size_y; y2++){ | ||
x1 = x2 = 0; | ||
while (x2 < size_x){ | ||
drzewa = sumPref[y2 + 1][x2 + 1] - sumPref[y1][x2 + 1] - sumPref[y2 + 1][x1] + sumPref[y1][x1]; | ||
if(drzewa < k) | ||
x2++; | ||
else if(drzewa >= k){ | ||
obwod = 2 * (x2 - x1 + 1) + 2 * (y2 - y1 + 1); | ||
if(obwod < minObw) | ||
minObw = obwod; | ||
x1++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
for(int i = 0; i < size_y + 1; i++) | ||
free(sumPref[i]); | ||
free(sumPref); | ||
|
||
return minObw; | ||
} | ||
|
||
int main(){ | ||
int size_x = 4, size_y = 4; | ||
int **t = (int**)malloc((unsigned int)size_y * sizeof(int*)); | ||
for(int i = 0; i < size_y; i++) | ||
t[i] = (int*)malloc((unsigned int)size_x * sizeof(int)); | ||
|
||
int tab[4][4] = { | ||
{1, 1, 1, 1}, | ||
{1, 1, 11, 1}, | ||
{1, 1, 1, 1}, | ||
{1, 1, 1, 15} | ||
}; | ||
|
||
for(int y = 0; y < size_y; y++) | ||
for(int x = 0; x < size_x; x++) | ||
t[y][x] = tab[y][x]; | ||
|
||
printf("%i\n", prostakat(t, size_y, size_x, 30)); | ||
|
||
for(int i = 0; i < size_y; i++) | ||
free(t[i]); | ||
free(t); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define x_size 6 | ||
#define y_size 4 | ||
|
||
int orzechy(int a[x_size][y_size], int k) { | ||
int sumPref[x_size + 1][y_size + 1]; | ||
|
||
//Obliczanie sumy prefiksowej | ||
for (int y = 0; y < y_size + 1; y++) { | ||
for (int x = 0; x < x_size + 1; x++) { | ||
if (x == 0 || y == 0) { | ||
sumPref[x][y] = 0; | ||
} else { | ||
sumPref[x][y] = a[x - 1][y - 1] + sumPref[x - 1][y] + | ||
sumPref[x][y - 1] - sumPref[x - 1][y - 1]; | ||
} | ||
} | ||
} | ||
|
||
int maks = 0; | ||
|
||
//Sprawdzanie wszystkich prostokątów zaczynających się w x i y | ||
for (int x = 0; x < x_size; x++) { | ||
for (int y = 0; y < y_size; y++) { | ||
for (int w = 1; w <= k && x + w <= x_size; w++) { | ||
if (k % w != 0) | ||
continue; | ||
|
||
int h = k / w; | ||
|
||
if (y + h > y_size) | ||
continue; | ||
|
||
int suma = sumPref[x + w][y + h] - sumPref[x][y + h] - | ||
sumPref[x + w][y] + sumPref[x][y]; | ||
|
||
if (suma > maks) | ||
maks = suma; | ||
} | ||
} | ||
} | ||
|
||
return maks; | ||
} | ||
|
||
int main() { | ||
int k; | ||
int t[x_size][y_size]; | ||
|
||
for (int y = 0; y < y_size; y++) | ||
for (int x = 0; x < x_size; x++) | ||
if(!scanf("%d", &t[x][y])) printf("wrong input"); | ||
|
||
if(!scanf("%d", &k)) printf("wrong input"); | ||
|
||
printf("%d", orzechy(t, k)); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
int blisko_zera(int v[], int n){ | ||
int l = 0, p = n - 1, m; | ||
int monotonicznosc = ((v[0] <= v[n - 1]) ? 1 : -1); | ||
while(l < p){ | ||
m = (l + p) / 2; | ||
if(v[m] * monotonicznosc < 0) | ||
l = m + 1; | ||
else | ||
p = m; | ||
} | ||
|
||
if(l > 0 && abs(v[l - 1]) < abs(v[l])) | ||
return abs(v[l - 1]); | ||
else | ||
return abs(v[l]); | ||
} | ||
|
||
|
||
|
||
int main() | ||
{ | ||
//main from zad_ms.c | ||
int n; | ||
if(!scanf("%d", &n)) printf("wrong input"); | ||
int *t = (int *)malloc((size_t)n * sizeof(int)); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
if(!scanf("%d", &t[i])) printf("wrong input"); | ||
} | ||
|
||
int nearestAbsZero = blisko_zera(t, n); | ||
printf("%d\n", nearestAbsZero); | ||
|
||
free(t); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include<stdio.h> | ||
|
||
int p(int n, int t[]){ | ||
int mini = 0, suma = 0, wynik = 0; | ||
|
||
for(int i = 0; i < n; i++){ | ||
suma += t[i]; | ||
if(suma < mini) mini = suma; | ||
if(suma - mini > wynik) wynik = suma - mini; | ||
} | ||
return wynik; | ||
} | ||
|
||
int main(){ | ||
int t[] = {1, 6, 8, -16, 20, -2, 7, -8}; | ||
int n = 8; | ||
printf("%i\n", p(n, t)); | ||
return 0; | ||
} |
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
Oops, something went wrong.