C language functions --- recursive and iterative

C language function brief introduction

C language function is a function used to compile the C language, where the library functions as ctype.h, is divided into classification functions, mathematical functions, catalog functions, process functions, diagnostic functions, operations, functions and so on.

On the surface, the function must bring the brackets when used, if necessary also pass parameters, the execution result of the function may be assigned to other variables

For library functions and custom functions

C language in the release package have been a lot better for us to function, they are placed in different categories with different headers (being asked to think so), the introduction of the corresponding header files when we can use the function. These functions are experts prepared, high efficiency, and taking into account the various boundary case, you readers ease of use.

C language library functions built-in function is called (Library Function). Library (Library) is a basic concept in programming, you can simply think it is a collection of functions, often on a disk folder. C language library that comes standard library called (Standard Library), other companies or individuals to develop a library called third-party libraries (Third-Party Library).

The difference between recursive and iterative

The basic concept of recursion: programming skills program that calls itself recursively called, is a function that calls itself.

A function call in its definition, directly or indirectly own a way, it is usually a large, complex problem into a problem similar to the original smaller problem to solve, can greatly reduce the amount of code. Recursive the ability is limited by an infinite set of statements to define the object.

Using recursion to note two things:

1) it is in the recursive procedure or function which calls itself;

2) When using recursion, you must have a clear recursive end condition, called recursive exports.

Iteration: the use of the original value of the variable calculate a new value if the variable is recursively calls itself, it is an iterative A constantly call B..

There must be a recursive iterations, but not necessarily recursive iterations, most are interchangeable. Iteration can not recursive, recursive call function, a waste of space, Recursion too deep and likely to cause a stack overflow.

Applications include recursive function with iterated function below give some examples of function calls.

  1. Implement a function, print multiplication tables, formulas table rows and columns themselves designated

Such as: 9 input, output 9 9 Table formulas, outputs 12, 12 output the multiplication tables 12.

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>
int main()
{
int i = 0, j = 0, input = 0;
scanf_s("%d", &input);
for (i = 1; i <= input; i++)
{
for (j = 1; j <= i; j++)
printf("%dx%d=%d ", i, j, i * j);
printf("\n");
}
return 0;
}
  1. Implementing a function to determine year is not a leap year.

Requirements: using the function to achieve the above-print 1000 to 2000 leap year.

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

void print_run_year(int year);

int main()

{

int year = 0;

for (year = 1000;year <= 2000;year++)

print_run_year(year);

return 0;

}

void print_run_year(int year)

{

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

printf("%d是闰年  ", year);

}




  1. Implement a function, a judgment is not a prime number.

Requirements: using the printing function implemented above the prime numbers between 100-200.

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

void print_sushu(int i);

int main()

{

int i = 0;

for (i = 100;i <= 200;i++)

print_sushu(i);

return 0;

}

void print_sushu(int i)

{

int j = 0;

for (j = 2;j < i;j++)

if (i % j == 0)

break;

else

{

printf("%d ", i);break;

}

}

Application of recursive and iterative functions.

  1. Recursive and non-recursive are seeking to achieve the n-th Fibonacci

E.g:

Input: Output 5: 5

Input: 10, Output: 55

Input: 2, Output: 1

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

int Fib(int i);

int main()

{

int i = 0;

int num = 0;

printf("输入要求的数字\n");

scanf_s("%d", &i);

num = Fib(i);

printf("%d\n", num);

return 0;

}

int Fib(int i)//递归写法

{

if (i <= 2)

return 1;

else

return Fib(i - 1) + Fib(i - 2);

}







#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

int Fib(int i);

int main()

{

int i = 0;

int num = 0;

printf("输入要求的数字\n");

scanf_s("%d", &i);

num = Fib(i);

printf("%d\n", num);

return 0;

}

int Fib(int i)//非递归写法

{

int j = 0, k = 0,m=0,n=0;

if (i <= 2)

return 1;

else

{

for (j = 1, k = 1, m = 1;k <= i - 2; k++, j = m, m = n)

{

n = j + m;

}

return n;

}

}


  1. Write a function of the k-th power of n, use recursive.

Requirements: k may be positive or negative

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

float print_nk(int n,int k);

int main()

{

int n = 0,k = 0;

float num = 0;

printf("输入分别为n的k次方\n");

scanf_s("%d%d", &n, &k);

num=print_nk(n, k);

printf("%f\n", num);

return 0;

}

float print_nk(int n, int k)

{

if (k == 0)

return 1;

else if (k > 0)

return n * print_nk(n, k - 1);

else

return (1.0 / n) * print_nk(n, k + 1);



}




  1. Write a recursive function DigitSum (n), enter a non-negative integer, it returns the number of the composition and

For example: call DigitSum (1729), it should return 1 + 2 + 7 + 9, and it is 19

Input: 1729, Output: 19

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

int DigitSum(int n);

int main()

{

int i = 0;

int num = 0;

scanf_s("%d", &i);

num = DigitSum(i);

printf("%d\n", num);

return 0;

}

int DigitSum(int n)

{

    int m=0;

if (n < 10)

return n;

else

m = n % 10;

return m + DigitSum(n / 10);

}


  1. 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.

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

void reverse_string(char * string,int length);

int main()

{

char str[] = "abcde";

int length = 5;

reverse_string(str,length);



return 0;

}

void reverse_string(char * string,int length)

{

if (length > 0)

{

printf("%c", string[length - 1]);

reverse_string(string, length - 1);

}

else

printf("\n");

}





#include<stdio.h>

int Sumstrlen(char *str);

int main()

{

int sum = 0;

char str[] = {0};

printf("输入字符串\n");

scanf("%s", str);

sum = Sumstrlen(str);

printf("字符个数为%d\n", sum);

return 0;

}

int Sumstrlen(char *str)

{

if (*str != '\0')

return 1 + Sumstrlen(str+1);

else

return 0;

}




  1. Recursive and non-recursive were achieved strlen.
#include<stdio.h>

int Sumstrlen(char *str);

int main()

{

int sum = 0;

char str[] = {0};

printf("输入字符串\n");

scanf("%s", str);

sum = Sumstrlen(str);

printf("字符个数为%d\n", sum);

return 0;

}

int Sumstrlen(char *str)

{

if (*str != '\0')

return 1 + Sumstrlen(str+1);

else

return 0;

}


This shows us that in our writing code is to use a function can greatly reduce the complexity of our code, appropriate use = recursive and iterative thinking function can help us to solve many complex problems.

Released nine original articles · won praise 16 · views 2384

Guess you like

Origin blog.csdn.net/Shangxingya/article/details/104149342