Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct liczba {
- int w;
- struct liczba *nast
- };
- typedef struct liczba *Liczba;
- Liczba reprezentacja(int n);
- int main() {
- reprezentacja(99);
- return 0;
- }
- Liczba reprezentacja(int n) {
- int pozycja = 0;
- Liczba *nast;
- Liczba *pom;
- Liczba *next;
- pom = (Liczba)malloc(sizeof(Liczba));
- // Jeśli n > 0 wstawiam na początek listy wartość 1.
- if(n > 0) {
- wstaw(1,pozycja);
- pozycja = next; next = pozycja->nast;
- }
- else {
- wstaw(-1,pozycja);
- pozycja = next; next = pozycja->nast;
- }
- // Wstawienie jako drugi element listy n.
- wstaw(n, pozycja);
- pozycja = next; next = pozycja->nast;
- int indeks = 0;
- while(n != 0) {
- // Jeśli n jest parzyste nic nie wpisujemy do listy.
- if(n%2 == 0) {
- n = n/2;
- indeks++;
- }
- // Jeśli (n+1) jest podzielne przez 4 to do listy wpisujemy wartość -1-indeks
- else if((a+1)%4 == 0) {
- n = (n + 1) / 2;
- wstaw(-1-indeks, pozycja);
- pozycja = next; next = pozycja->nast;
- indeks++;
- }
- else if((a-1)%4 == 0) {
- n = (n - 1) / 2;
- wstaw(indeks, pozycja);
- pozycja = next; next = pozycja->nast;
- }
- }
- }
- void wstaw(Lista *pozycja, int x) {
- pom->nast = pozycja->nast;
- pom->w = x;
- pozycja->nast = pom;
- pozycja = next; next = pozycja->nast;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement