Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <memory.h>
- int fib(int count)
- {
- if (count < 2) {
- return 1;
- }
- return fib(count - 1) + fib(count - 2);
- }
- char *fibstr(int n)
- {
- char *x = (char*)malloc((fib(n) + 1) * sizeof(char));
- char *p = (char*)malloc((fib(n) + 1) * sizeof(char));
- char *pp = (char*)malloc((fib(n) + 1) * sizeof(char));
- char aa[2];
- aa[0] = 'a';
- char bb[2];
- bb[0] = 'b';
- char *a = &aa[0];
- char *b = &bb[0];
- if (n == 1) {
- free(p);
- free(pp);
- free(x);
- return a;
- }
- if (n == 2) {
- free(p);
- free(pp);
- free(x);
- return b;
- }
- int i;
- pp[0] = aa[0];
- p[0] = bb[0];
- for (i = 3; i < (n + 1); i++) {
- x[0] = 0;
- strcat(x, p);
- p[0] = 0;
- strcat(p, strcat(pp, x));
- pp[0] = 0;
- strcat(pp, x);
- }
- free(pp);
- free(x);
- return p;
- }
- int main()
- {
- int n;
- scanf("%d", &n);
- char *res = fibstr(n);
- printf("%s", res);
- if (n > 2) free(res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement