1. The non-recursive and recursive are seeking to achieve the n-th Fibonacci number.
Using recursive function to implement, the disadvantage is inefficient, from scratch every time call
example:
// 50
// 49 48
// 48474746
#include<stdlib.h>
#include<stdio.h>
//1 1 2 3 5 8 13 21 34 55......
int fib(int n)//利用递归函数求取
{
if (n <= 2)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
int main()
{
int n = 0;
int ret = 0;
printf("求第n个斐波那契数:\n");
scanf("%d", &n);
ret=fib(n);
printf("%d\n", ret);
system("pause");
return 0;
}
Definition of a, b, c, a number of each calculation, a backward movement of the abc, avoiding double counting
#include<stdlib.h>
#include<stdio.h>
//1 1 2 3 5 8 13 21 34 55
//a b c
int fib(int n)
{
int a = 1;
int b = 1;
int c = 1;
while (n > 2)
{
c = a + b;
a = b;
b = c;
n--;
}
return c;
}
2. Write a function to achieve the calculated n ^ k
#include<stdlib.h>
#include<stdio.h>
int Index(int n, int k)
{
if (k == 1)
return n;
else
return n*Index(n, k - 1);
}
int main()
{
int n;
int k;
int ret;
printf("请分别输入数字n和指数k\n");
scanf_s("%d,%d", &n, &k);
ret= Index(n, k);
printf("ret=%d\n", ret);
system("pause");
return 0;
}
- Write a recursive function DigitSum (n), enter a non-negative integer, it returns the sum of the numbers and the composition,
e.g., call DigitSum (1729), it should return 1 + 2 + 7 + 9, and it is 19
#include<stdlib.h>
#include<stdio.h>
void DigitSum(int n,int* sum)
{
if (n > 9)
{
DigitSum(n / 10, sum);
}
*sum += n % 10;
}
//1729
//(172) 9
//(17) 2 9
//1 7 2 9
int main()
{
int num;
int sum=0;
printf("请输入一个非负整数:\n");
scanf("%d", &num);
DigitSum(num, &sum);
printf("%d", sum);
system("pause");
return 0;
}
- Write a function reverse_string (char * string) (recursive)
implementation: the parameter string of characters oppositely oriented.
Requirements: You can not use string manipulation functions in the C library.
#include<stdlib.h>
#include<stdio.h>
void reverse_string(char * string)
{
if (*(string) != '\0')
{
reverse_string(string + 1);
}
printf("%c", *(string-1));
}
int main()
{
char arr[] = "abcd";
reverse_string(arr);
printf("\n");
system("pause");
return 0;
}
The non-recursive and recursive were achieved strlen
#include<stdio.h>
#include<stdlib.h>
int Strlen(char* arr)//递归的方式实现
{
if (*(arr) != '\0')
{
return Strlen(++arr)+1;
}
return 0;
}
int len(char* arr)//非递归的方式
{
int count = 0;
while (*(arr + count) != '\0')
{
count++;
}
return count;
}
int main()
{
int ret;
char arr[] = "abcd";
ret=Strlen(arr);
printf("%d\n", ret);
ret = len(arr);
printf("%d\n", ret);
system("pause");
return 0;
}
6. The non-recursive and recursive are seeking to achieve the factorial of n
#include<stdio.h>
#include<stdlib.h>
int Fac(int i)//用递归函数实现n的阶乘
{
if (i <= 1)
{
return 1;
}
return i*Fac(i - 1);
}
int fac(int i)//用非递归函数实现
{
int n = 1;
int ret = 1;
for (n = 1; n <= i; ++n)
{
ret = ret*n;
}
return ret;
}
int main()
{
int i;
printf("请输入想要所求数字n的阶乘:\n");
scanf("%d", &i);
Fac(i);
printf("%d\n", Fac(i));
fac(i);
printf("%d\n", fac(i));
system("pause");
return 0;
}
7. recursive manner every print an integer
#include<stdio.h>
#include<stdlib.h>
int Print(int n)
{
if (n > 9)
{
Print(n/10);
}
printf("%d ", n % 10);
}
int main()
{
int i;
printf("请输入一个整数:\n");
scanf_s("%d", &i);
Print(i);
system("pause");
return 0;
}