-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d706107
commit 02d8ed5
Showing
5 changed files
with
291 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
|
||
// zwraca min(a,b) | ||
int minimum(int a, int b) { | ||
return a < b ? a : b; | ||
} | ||
|
||
|
||
// zwraca liczbe roznych elementow w niemalejacej a[n] | ||
int distinct(int n, int a[]) { | ||
int count = 0; | ||
if (n) { | ||
int val = a[0]; | ||
++count; | ||
for (int i = 1; i < n; ++i) { | ||
if (a[i] != val) { | ||
val = a[i]; | ||
++count; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
|
||
// zwraca liczbe roznych elementow w sumie a[n] oraz b[m] | ||
// przy zalozeniu, ze na wejscie sa posortowane niemalejaco | ||
int countDistinct(int n, int a[], int m, int b[]) { | ||
if (!n) | ||
return distinct(m, b); | ||
if (!m) | ||
return distinct(n, a); | ||
int val = minimum(a[0], b[0]); // m, n > 0 | ||
int i = 0, j = 0; | ||
while (a[i] == val) ++i; | ||
while (b[j] == val) ++j; | ||
return 1 + countDistinct(n - i, a + i, m - j, b + j); | ||
} | ||
|
||
|
||
/*** rzeczy do testow ***/ | ||
|
||
// wczytaj tablice | ||
int* getArray(int n) { | ||
int* s = (int*)malloc((unsigned)n * sizeof(int)); | ||
for(int i = 0; i < n; ++i) | ||
scanf("%d", s + i); | ||
return s; | ||
} | ||
|
||
int main(void) { | ||
|
||
// wczytaj n i a[n] | ||
int n; | ||
scanf("%d", &n); | ||
int *a = getArray(n); | ||
|
||
// wczytaj m i b[m] | ||
int m; | ||
scanf("%d", &m); | ||
int *b = getArray(m); | ||
|
||
// licz moc sumy | ||
printf("%d\n", countDistinct(n, a, m, b)); | ||
|
||
free(a); | ||
free(b); | ||
|
||
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,10 @@ | ||
// zad. 10 tablice | ||
|
||
/* | ||
modyfikujemy zabawkę: | ||
jeśli było coś węższego powyżej, zawężamy wszystko powyżej | ||
(tak jakbyśmy świecili latarką od góry zabawki i patrzyli tylko na jasne obszary) | ||
*/ |
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,72 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// typ ciag (dlugosc oraz wskaznik na tablice zawierajaca elementy) | ||
typedef struct { | ||
int dl; | ||
double* el; | ||
} ciag; | ||
|
||
// konstruuje ciag roznicowy danego i zwraca adres poczatku | ||
ciag* diffSeq(ciag *s) { | ||
int n = s->dl; | ||
ciag* diff = malloc(sizeof(ciag)); | ||
diff->dl = n - 1; | ||
double* diffEl = malloc((unsigned)(n - 1) * sizeof(double)); | ||
for(int i = n - 2; i >= 0; --i) | ||
diffEl[i] = (s->el)[i + 1] - (s->el)[i]; | ||
diff->el = diffEl; | ||
return diff; | ||
} | ||
|
||
|
||
// typ ciagCiagow: wskaznik na ciag oraz na kolejny ciagCiagow | ||
typedef struct ciagCiag { | ||
ciag* c; | ||
struct ciagCiag* nast; | ||
} ciagCiagow; | ||
|
||
ciagCiagow* diffSeqs(ciag *s) { | ||
ciagCiagow* diffs = malloc(sizeof(ciagCiagow)); | ||
diffs->c = s; | ||
diffs->nast = (s->dl == 1) ? NULL : diffSeqs(diffSeq(s)); | ||
return diffs; | ||
} | ||
|
||
int main(void) { | ||
|
||
int n = 5; | ||
|
||
ciag *s = malloc(sizeof(ciag)); | ||
s->dl = 5; | ||
double *d = malloc(5 * sizeof(double)); | ||
//for(int i = 0; i < n; ++i) | ||
// d[i] = i + 0.0; | ||
d[0] = 1.0; | ||
d[1] = 4.0; | ||
d[2] = 10.0; | ||
d[3] = 20.0; | ||
d[4] = 35.0; | ||
|
||
s->el = d; | ||
|
||
ciag *t = diffSeq(s); | ||
for(int i = 0; i < n - 1; ++i) | ||
printf("%f ", (t->el)[i]); | ||
printf("\n"); | ||
|
||
ciagCiagow *ss = diffSeqs(s); | ||
while (ss) { | ||
for(int i = 0; i < (ss->c)->dl; i++) | ||
printf("%f ", (ss->c)->el[i]); | ||
printf("\n"); | ||
ss = ss->nast; | ||
} | ||
|
||
free(d); | ||
free(t); | ||
free(s); | ||
free(ss); // to powinno byc madrzejsze (jakas procedura iteracyjnie zwalniaca ss od konca) | ||
|
||
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,107 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
bool neg2_of_int(long long int n, int wynik[], int size) { | ||
if (!size) { | ||
return false; | ||
} | ||
if (n == 0) { | ||
wynik[0] = -1; | ||
return true; | ||
} | ||
if (n > 0) { | ||
wynik[0] = (int)(n % 2); | ||
return neg2_of_int(- n / 2, wynik + 1, size - 1); | ||
} | ||
else { | ||
wynik[0] = (int)(-n % 2); | ||
return neg2_of_int((1 - n) / 2, wynik + 1, size - 1); | ||
} | ||
} | ||
|
||
/*** ponizej okropnie slaba proba zrobienia +1 z uwzglednieniem roznych przypadkow brzegowych ***/ | ||
|
||
|
||
|
||
/* przepisuje tablice cyfra[] do tablicy wynik[size] | ||
zwraca falsz, jesli sie nie miesci */ | ||
bool rewrite(int cyfra[], int wynik[], int size) { | ||
if (size <= 0) | ||
return false; | ||
int i = 0; | ||
while (i < size && cyfra[i] != -1) { | ||
wynik[i] = cyfra[i]; | ||
++i; | ||
} | ||
if (i < size) | ||
wynik[i] = -1; | ||
return (i != size); | ||
} | ||
|
||
/* wpisuje zapis (-2)-owy liczby n + 1 na tablice wynik | ||
zwraca falsz jesli wynik nie zmiescil sie w tablicy */ | ||
bool inc(int cyfra[], int wynik[], int size) { | ||
|
||
// nie ma gdzie wpisywac --- zwroc falsz | ||
if (size <= 0) | ||
return false; | ||
|
||
// obsluga przypadku gdy cyfra[] przechowuje zero | ||
if (cyfra[0] == -1) { | ||
wynik[0] = 1; | ||
if (size > 1) { | ||
wynik[1] = -1; | ||
return true; | ||
} | ||
else | ||
return false; | ||
} | ||
|
||
// koniec 0 -> dodaj 1 | ||
if (cyfra[0] == 0) { | ||
wynik[0] = 1; | ||
// przepisz reszte liczby z cyfra[] do wynik[] (jesli sie miesci) | ||
return rewrite(cyfra + 1, wynik + 1, size - 1); | ||
} | ||
|
||
// koniec 11 -> zmien na 00 | ||
if (cyfra[0] == 1 && cyfra[1] == 1) { | ||
wynik[0] = 0; | ||
bool b = false; | ||
if (size > 1) | ||
wynik[1] = 0; | ||
b = rewrite(cyfra + 2, wynik - 2, size - 2); | ||
// obsluga przypadku -1 + 1 =0 | ||
if (wynik[2] == -1) { | ||
wynik[0] = -1; | ||
b = size > 0; | ||
} | ||
return b; | ||
} | ||
|
||
// w przec. przyp. (koniec 01) -> zmien na 10 i dodaj 1 do prefiksu kodlugosci 2 | ||
if (cyfra[0] == 1 && cyfra[1] != 1) { | ||
// tu sie poddalem | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
/*** do testow ***/ | ||
|
||
int main(void) { | ||
|
||
long long int n = -10; | ||
int size = 10; | ||
int* wynik = malloc((unsigned)n * sizeof(int)); | ||
|
||
printf("%d\n", neg2_of_int(n, wynik, size)); | ||
for(int i = 0; (i < size) && (wynik[i] != -1); ++i) | ||
printf("%d", wynik[i]); | ||
printf("\n"); | ||
|
||
free(wynik); | ||
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 |
---|---|---|
@@ -1,10 +1,32 @@ | ||
// zad. 10 tablice | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
/* | ||
modyfikujemy zabawkę: | ||
jeśli było coś węższego powyżej, zawężamy wszystko powyżej | ||
(tak jakbyśmy świecili latarką od góry zabawki i patrzyli tylko na jasne obszary) | ||
// rurka[0] to srednica otworu tuz przy dnie rurki | ||
// krazki[0] to srednica krazka wrzuconego najpierw | ||
int wrzucaj(int n_rurka, int rurka[], int n_krazki, int krazki[]) { | ||
if (!n_rurka || !n_krazki) | ||
return 0; | ||
int szerokosc = rurka[n_rurka - 1]; | ||
// swiecimy do rurki od gory i zmieniamy szerokosc na oswietlona | ||
for(int i = n_rurka - 1; i >= 0; --i) { | ||
if (rurka[i] > szerokosc) | ||
rurka[i] = szerokosc; | ||
else | ||
szerokosc = rurka[i]; | ||
} | ||
int k = 0; // ile krazkow juz sie zmiescilo | ||
for(int i = 0; (i < n_rurka && k < n_krazki); ++i) { | ||
if (krazki[k] <= rurka[i]) | ||
++k; | ||
} | ||
return k; | ||
} | ||
|
||
/** do testow **/ | ||
|
||
*/ | ||
int main(void) { | ||
int r[10] = {1, 4, 3, 2, 5, 6, 7, 5, 7, 6}; | ||
int k[8] = {1, 2, 2, 2, 5, 2, 6, 1}; | ||
printf("%d\n", wrzucaj(10, r, 8, k)); | ||
return 0; | ||
} |