Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.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));
- pp[0] = 'a';
- p[0] = 'b';
- if (n == 1) {
- free(p);
- free(x);
- return pp;
- }
- if (n == 2) {
- free(pp);
- free(x);
- return p;
- }
- int i;
- 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);
- free(res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement