Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
witek-formanski committed Nov 14, 2023
2 parents 6d68d36 + 5199b1e commit 93f83e3
Show file tree
Hide file tree
Showing 24 changed files with 372 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ _WDP\* MIM UW_
- [next_perm](./src/cw3/zad12/next_perm.c): autor: @lbozyk (C) :white_check_mark:
- [zad. 14.] sprawdź, czy jeden ciąg zawiera się w drugim
- [subsequence](./src/cw3/zad14/subsequence.c) (C) :white_check_mark::microscope:
- [zad. 16.] ciąg róznicowy
- [zad. 16.] ciąg różnicowy
- [differential_sequence](./src/cw3/zad16/differential_sequence.c) (C)
- [diff_seq](./src/cw3/zad16/diff_seq.c) (C): autor: @lbozyk (C) :white_check_mark:
- [diff_seq_fam](./src/cw3/zad16/diff_seq_fam.c) (C) :white_check_mark::microscope:
- [zad. 17.] ciąg ciągu różnicowego
- [diff_diff_seq_fam](./src/cw3/zad17/diff_diff_seq_fam.c) (C) :white_check_mark::microscope:

### Laboratorium II: [Gra w skakanie (NWD)](./pdf/WDP_.Inf.23_24Z__Zadanie_rozgrzewkowe_2.pdf) 2023.10.11

Expand All @@ -100,6 +103,7 @@ _WDP\* MIM UW_
- [zad. 2.]
- [absolute_minimum_of_sequence](./src/cw5/zad2/absolute_minimum_of_sequence.cpp) (C++)
- [zad_2](./src/cw5/zad2/zad_ms.c): autor: @MrD4rkne (C) :white_check_mark::microscope:
- [zad2](./src/cw5/zad2/zad2_pxl.c): autor: @pixelkubek (C) :white_check_mark::microscope:
- [zad. 3.]
- [minimal_sum_of_two_elements](./src/cw5/zad3/minimal_sum_of_two_elements.c) (C) :white_check_mark:
- [minimal_sum_of_two_elements_brute](./src/cw5/zad3/minimal_sum_of_two_elements_brute.c) (C) :white_check_mark:
Expand All @@ -113,6 +117,7 @@ _WDP\* MIM UW_
- [diagonal](./src/cw5/zad6/diagonal.c) (C)
- [zad. 7.]
- [zad_7](./src/cw5/zad7/zad_ms.c): autor: @MrD4rkne (C) :white_check_mark::microscope:
- [zad7](./src/cw5/zad7/zad7.c): autor: @pixelkubek (C) :white_check_mark:
- [zad. 8.]
- [zad_8](./src/cw5/zad8/zad_ms.c): autor: @MrD4rkne (C) :white_check_mark::microscope:
- [zad. 10.]
Expand All @@ -127,6 +132,9 @@ _WDP\* MIM UW_
- [daleko](./src/cw5/zad14/daleko_ms.c): autor: @MrD4rkne (C) :white_check_mark::microscope:
- [zad. 15.]
- [zad_15](./src/cw5/zad15/zad_ms.c): autor: @MrD4rkne (C) :white_check_mark::microscope:
- [zad15](./src/cw5/zad15/zad15.c): autor: @pixelkubek (C) :white_check_mark:
- [zad. 17.]
- [orzechy](./src/cw5/zad17/orzechy.c) (C) :white_check_mark::microscope:
- [zad. 18.]
- [zad18](./src/cw5/Zad18/zad18.c): autor: @pixelkubek (C) :white_check_mark:

Expand Down
36 changes: 36 additions & 0 deletions gen05.c
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;
}
23 changes: 23 additions & 0 deletions spr.sh
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
42 changes: 42 additions & 0 deletions src/cw3/zad16/diff_seq_fam.c
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));
}
51 changes: 51 additions & 0 deletions src/cw3/zad17/diff_diff_seq_fam.c
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));
}
70 changes: 70 additions & 0 deletions src/cw5/zad15/zad15.c
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;
}
59 changes: 59 additions & 0 deletions src/cw5/zad17/orzechy.c
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));
}
38 changes: 38 additions & 0 deletions src/cw5/zad2/zad2_pxl.c
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);
}
19 changes: 19 additions & 0 deletions src/cw5/zad7/zad7.c
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;
}
2 changes: 1 addition & 1 deletion src/kol1_22/zad1/schodki.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// M(n)

int schodki(int n, int t[]) {
int *pom = (int*)malloc((size_t)(n+1)*sizeof(int));
int *pom = (int*)calloc((size_t)(n+1), sizeof(int));
int ile = 0;

for(int i=0; i<n; i++) {
Expand Down
Loading

0 comments on commit 93f83e3

Please sign in to comment.