daily pastebin goal
78%
SHARE
TWEET

Untitled

a guest Oct 23rd, 2018 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <memory.h>
  4.  
  5. int fib(int count)
  6. {
  7.     if (count < 2) {
  8.         return 1;
  9.     }
  10.     return fib(count - 1) + fib(count - 2);
  11. }
  12.  
  13. char *fibstr(int n)
  14. {
  15.     char *x = (char*)malloc((fib(n) + 1) * sizeof(char));
  16.     char *p = (char*)malloc((fib(n) + 1) * sizeof(char));
  17.     char *pp = (char*)malloc((fib(n) + 1) * sizeof(char));
  18.     char aa[2];
  19.     aa[0] = 'a';
  20.     char bb[2];
  21.     bb[0] = 'b';
  22.     char *a = &aa[0];
  23.     char *b = &bb[0];
  24.     if (n == 1) {
  25.         free(p);
  26.         free(pp);
  27.         free(x);
  28.         return a;
  29.  
  30.     }
  31.     if (n == 2) {
  32.         free(p);
  33.         free(pp);
  34.         free(x);
  35.         return b;
  36.     }
  37.     int i;
  38.     pp[0] = aa[0];
  39.     p[0] = bb[0];
  40.     for (i = 3; i < (n + 1); i++) {
  41.         x[0] = 0;
  42.         strcat(x, p);
  43.         p[0] = 0;
  44.         strcat(p, strcat(pp, x));
  45.         pp[0] = 0;
  46.         strcat(pp, x);
  47.     }
  48.     free(pp);
  49.     free(x);
  50.     return p;
  51. }
  52.  
  53. int main()
  54.  
  55. {
  56.     int n;
  57.     scanf("%d", &n);
  58.     char *res = fibstr(n);
  59.     printf("%s", res);
  60.     if (n > 2) free(res);
  61.     return 0;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top