Advertisement
Guest User

Kabanosy

a guest
Jan 16th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct liczba {
  5. int w;
  6. struct liczba *nast
  7. };
  8. typedef struct liczba *Liczba;
  9.  
  10. Liczba reprezentacja(int n);
  11.  
  12. int main() {
  13. reprezentacja(99);
  14. return 0;
  15. }
  16.  
  17. Liczba reprezentacja(int n) {
  18. int pozycja = 0;
  19. Liczba *nast;
  20. Liczba *pom;
  21. Liczba *next;
  22. pom = (Liczba)malloc(sizeof(Liczba));
  23. // Jeśli n > 0 wstawiam na początek listy wartość 1.
  24. if(n > 0) {
  25. wstaw(1,pozycja);
  26. pozycja = next; next = pozycja->nast;
  27. }
  28. else {
  29. wstaw(-1,pozycja);
  30. pozycja = next; next = pozycja->nast;
  31. }
  32. // Wstawienie jako drugi element listy n.
  33. wstaw(n, pozycja);
  34. pozycja = next; next = pozycja->nast;
  35. int indeks = 0;
  36. while(n != 0) {
  37. // Jeśli n jest parzyste nic nie wpisujemy do listy.
  38. if(n%2 == 0) {
  39. n = n/2;
  40. indeks++;
  41. }
  42. // Jeśli (n+1) jest podzielne przez 4 to do listy wpisujemy wartość -1-indeks
  43. else if((a+1)%4 == 0) {
  44. n = (n + 1) / 2;
  45. wstaw(-1-indeks, pozycja);
  46. pozycja = next; next = pozycja->nast;
  47. indeks++;
  48. }
  49. else if((a-1)%4 == 0) {
  50. n = (n - 1) / 2;
  51. wstaw(indeks, pozycja);
  52. pozycja = next; next = pozycja->nast;
  53. }
  54. }
  55. }
  56.  
  57. void wstaw(Lista *pozycja, int x) {
  58. pom->nast = pozycja->nast;
  59. pom->w = x;
  60. pozycja->nast = pom;
  61. pozycja = next; next = pozycja->nast;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement