C language learning summary _04

1. In the previous article, when using recursion to find the nth Fibonacci number, the efficiency is actually very low. Because there are many repeated calculations, as shown below:
Insert picture description here
When finding the sixth number, many repeated calculations are performed.

int count = 0;
//用递归求第n个斐波那契数字
int Fib(int n){
    
    
	if (1 == n || 2 == n){
    
    
		return 1;
	}
	if (n == 3){
    
    
		count++;
	}
	return Fib(n - 1) + Fib(n - 2);
}
int main(){
    
    
	int ret= Fib(40);
	printf("%d\n", ret);
	printf("调用Fib(3) %d 次\n", count);
	system("pause");
	return 0;
}

Insert picture description here
Only calculating Fib(40) has calculated Fib(3) so many times, so why is it so inefficient to call a function because it costs to call a function, because it will continue to form and release stack frames, which is reflected in time And space. So how can it be improved? You can change recursion to non-recursion, using iterative methods.

int fib_1(int n)
{
    
    
	int first = 1;
	int second = 1;
	int third = 1;
	while (n>2){
    
    
		third = first + second;
		first = second;
		second = third;
		n--;
	}
	return third;
}
int main(){
    
    

	int ret = fib_1(40);
	printf("%d\n", ret);
	

	system("pause");
	return 0;
}

The result of this iterative method is seconds.
2.
1. Array
Precautions when using array
1) int arr[25]; To
create an array, a constant must be given in [], and variables cannot be used.
2) The array can be initialized as a whole, but cannot be assigned as a whole.
3) The storage of the array in the memory. The
array space in the memory is opened and released as a whole. The array must be regarded as a whole. The subscript starts from zero and the address starts from low. To high, the basic unit of memory addressing is bytes. The array must be stored continuously in the memory (linear storage); write a piece of code to see the address of the array in the element:

int main(){
    
    
	int arr[10] = {
    
     0 };
	int arrsize = sizeof(arr) / sizeof(arr[0]);//此时数组名,sizeof(arr)单独使用 \
	代表的是整个数组
	for (int i = 0; i < arrsize; i++){
    
    
		printf("&arr[%d] = %p\n", i, &arr[i]);
	}
	system("pause");
	return 0;
}

Insert picture description here
You can see that the array element addresses are from high to low and are continuous, because the size of each int type is 4 bytes.
2. Extend to two-dimensional arrays
Two-dimensional arrays can be regarded as one-dimensional arrays, but the elements stored in the array are one-dimensional arrays, and the same is true for three-dimensional and four-dimensional arrays. The analogy is that the numbers stored in the array are, for example, int type numbers. It is only an array type, and will be composed of structure types, classes, etc. in the future.
Note: When initializing a two-dimensional or multi-dimensional array, only the length of the first dimension can be omitted.
3. Array as a parameter of a function. When
an array is passed as a parameter of a function, dimensionality reduction occurs, and the dimensionality is reduced to a pointer to its internal element. When the array is passed as a parameter, the length of the array must also be passed in.
Find the index of an element in an unordered array
Step 1: Sort the array first
Step 2: Find the index of the array binary The
code is as follows:

//打印数组元素
void PrintArr(int arr[], int arrsize){
    
    
	for (int i = 0; i < arrsize; i++){
    
    
		printf("%d\n", *(arr + i));
		//printf("%d\n", arr[i]);
	}
}
//冒泡排序
void BubbleSort(int arr[], int arrsize){
    
    
	int flag = 0;
	for (int i = 0; i < arrsize-1; i++){
    
    
		for (int j = 0; j < arrsize - 1 - i; j++){
    
    
			if (arr[j]>arr[j + 1]){
    
    
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
				flag = 1;
			}
		}
		if (flag){
    
    
			break;
		}
	}
}
//二分查找
int BinSearch(int arr[], int arrsize, int num){
    
    
	int left = 0;
	int right = arrsize - 1;//[]
	int mid = 0;
	while (left <= right){
    
    
		mid = left + (right - left) / 2;//避免溢出
		if (num > arr[mid]){
    
    
			left = mid + 1;
		}
		else if (num <arr[mid]){
    
    
			right = mid - 1;
		}
		else{
    
    
			return mid;
		}
	}
	if (left > right){
    
    
		printf("没有这个元素~~\n");
	}

}
int main(){
    
    
	int arr[] = {
    
     0,1,2,3,4,99,66,77,88,111,222,555,666 };
	int arrsize = sizeof(arr) / sizeof(arr[0]);

	printf("排序前:\n");

	PrintArr(arr, arrsize);

	BubbleSort(arr, arrsize);

	printf("排序后:\n");

	PrintArr(arr, arrsize);

	int index  = BinSearch(arr, arrsize, 66);
	printf("要查找元素的下标为 %d:\n", index);

	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/CZHLNN/article/details/109270265