Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIZE 100
  5.  
  6. typedef unsigned long ulong;
  7.  
  8. ulong max(ulong *seq, int len){
  9.     ulong top = 0;
  10.  
  11.     while(--len){
  12.         if(top < *(seq+len)) top = *(seq+len);
  13.     }
  14.  
  15.     return top;
  16. }
  17.  
  18. void calc_seq(ulong *seq, int len){
  19.     int i = *(seq+1) = *seq = 1;
  20.  
  21.     while( (i<<1)+1 < len) {
  22.         *(seq+(i<<1))         = *(seq+i)+*(seq+i-1);
  23.         *(seq+(((i++)<<1)+1)) = *(seq+i);
  24.     }
  25.  
  26.     if((len%2) != 0){
  27.         *(seq+(i<<1)) = *(seq+i)+*(seq+i-1);
  28.     }
  29. }
  30.  
  31. ulong calc_max_in_sec(ulong *seq, int len){
  32.     calc_seq(seq, len);
  33.     return max(seq,len);
  34. }
  35.  
  36. int main()
  37. {
  38.     // stack allocation
  39.     ulong tab[SIZE];
  40.     printf("stack: %li\n", calc_max_in_sec(tab, SIZE));
  41.  
  42.     // heap allocation
  43.     ulong *tab_heap = malloc(sizeof(ulong) * SIZE);
  44.     printf("heap: %li\n", calc_max_in_sec(tab, SIZE));
  45.     free(tab_heap);
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement