Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.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. pp[0] = 'a';
  19. p[0] = 'b';
  20. if (n == 1) {
  21. free(p);
  22. free(x);
  23. return pp;
  24. }
  25. if (n == 2) {
  26. free(pp);
  27. free(x);
  28. return p;
  29. }
  30. int i;
  31. for (i = 3; i < (n + 1); i++) {
  32. x[0] = 0;
  33. strcat(x, p);
  34. p[0] = 0;
  35. strcat(p, strcat(pp, x));
  36. pp[0] = 0;
  37. strcat(pp, x);
  38. }
  39. free(pp);
  40. free(x);
  41. return p;
  42. }
  43.  
  44. int main()
  45.  
  46. {
  47. int n;
  48. scanf("%d", &n);
  49. char *res = fibstr(n);
  50. printf("%s", res);
  51. free(res);
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement