Skip to content

Commit

Permalink
Added zad3 and change zad9 from c++ to c
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelkubek committed Nov 14, 2023
1 parent e292b45 commit 89e8195
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ _WDP\* MIM UW_

### Ćwiczenia VI: [Stosy, kolejki i koszt zamortyzowany](./pdf/WDP_.Inf.23_24Z__Stosy,_kolejki_i_koszt_zamortyzowany.pdf) 2023.11.13

- [zad. 3.]
- [zad_3](./src/cw6/zad3/zad3_sadzawka.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad. 7.]
- [zad_7](./src/cw6/zad7/zad7_skyline.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad. 9.]
- [zad_9](./src/cw6/zad9/zad9_Zeckendorf.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad_9](./src/cw6/zad9/zad9_Zeckendorf.c): autor: @pixelkubek (C) :white_check_mark:

## Kolokwia

Expand Down
61 changes: 61 additions & 0 deletions src/cw6/zad3/zad3_sadzawka.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<stdio.h>
#include<stack>

using namespace std;

int znak(int n){
return n >= 0 ? 1 : -1;
}

int warstwy(int t[], int n){
stack<int> s;
int w1, doZmiany;
for(int i = 0; i < n; i++){
if(s.empty() && t[i] < 0){
s.push(t[i]);
} else if(!s.empty()){
w1 = 0;
doZmiany = t[i];

while (znak(doZmiany) == znak(t[i]) && !s.empty()) {
if(znak(s.top()) == znak(t[i]))
w1 += s.top();
else{
w1 += -s.top();
doZmiany += s.top();
}
s.pop();
}
w1 += doZmiany;

if(!(s.empty() && w1 >= 0 && doZmiany >= 0)){
if(doZmiany) s.push(doZmiany);
if(w1) s.push(w1);
}
}
}

int ct = 0;
while(!s.empty())
{
t[ct++] = s.top();
s.pop();
}

return ct;

}

void printArr(int t[], int n){
for(int i = 0; i < n; i++)
printf("%i ", t[i]);
printf("\n");
}

int main(){
int temperatury[] = {2, -12, 8, -4, 2, 4, -4, 1, -3};
int n = 9;
int n_wynik = warstwy(temperatury, n);
printArr(temperatury, n_wynik);
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include<stdlib.h>
#include<stdio.h>

using namespace std;

void printArr(int t[], int n){
for(int i = 0; i < n; i++)
printf("%i ", t[i]);
Expand Down

0 comments on commit 89e8195

Please sign in to comment.