The first official content, come on

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

This article introduces the use of the sleep() function, the demonstration of the precautions for binary search, and the simplified method of outputting the sum of factorials

1. Use of the Sleep() function

To use Sleep(), you need to add the [windows.h] header file
Sleep is to make the program pause for a certain period of time after encountering him and continue to execute. As for the pause time,
it is related to the value in (), and the unit is milliseconds, that is, Sleep(1000) is to pause for 1 second
Example:
Before adding the Sleep function:
insert image description here

The running result after adding the Sleep function:
insert image description here
After inserting, you can clearly see the running status line by line.
The overall code is as follows:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<windows.h>
/ / Write code that demonstrates that multiple characters move from both ends and converge toward the middle.
int main()
{ char arr1[] = "welcome to you!!!"; char arr2[] = "********************"; int left = 0; int right = strlen(arr2) - 1; while (left <= right) { arr2[left] = arr1[left]; arr2[right] = arr1[right]; printf("%s\n", arr2); Sleep(1000);//Sleep (wait) for 1 second before continuing //system(“cls”);//clear screen left++; right–; } //printf(“%s\n”, arr2 );














return 0;

}

2. Dichotomy to find elements

First of all, you must pay attention to the prerequisites for using the binary search:
you must search in an ordered sequence, and return the subscript of the target element after finding it!
Code demonstration:
//Search the target element in the ascending array of 1-10, find and return its subscript value, otherwise the output is not found
int main()
{ int arr[] = { 1,2,3,4,5,6 ,7,8,9,10 }; int k = 0; scanf(“%d”, &k); int length = (sizeof(arr) / sizeof(arr[0])); int left = 0;// Initialize the leftmost subscript int right = length - 1;//Initialize the rightmost subscript int flag = 0;//Define it to judge if the execution is not found int mid = (left + right) / 2;//Initialize the middle Subscript //loop judgment condition: if the left subscript is greater than the right subscript, it is not found, and the loop ends while (left <= right) { if (arr[mid] < k) { left = mid + 1; mid = (left + right ) / 2; } else if (arr[mid] > k) { right = mid - 1;



















mid = (left + right) / 2;
}
else
{ printf("found, the subscript is: %d\n", mid); flag = 1; break; } } // If not found, the value of flag is not Change, execute this if statement if (flag == 0) { printf("Not found!\n"); } return 0; } The result display:













insert image description here

3. Calculate the sum of factorials from 1-10

The title of this question is relatively simple. To calculate the sum of the factorials of 1-10, you must first think of using two for loops. However, if you use one for loop to process the calculation, wouldn't it greatly reduce the time complexity.
Let's first show the method that most people will think of at the first time:
insert image description here
This method also has a point to pay attention to: use the ret variable to store the factorial of each number and add it to the sum and set it to 1, otherwise it will be A multi-multiplication error occurred.
The following shows the method of using only one for loop:

insert image description here
Here, we add factorial to each number and add it to sum by the way , and the subsequent number is only multiplied by itself on the basis of the previous ret . This method is recommended. At the same time, we must pay more attention to thinking when writing code. Can think of an easier way!

Summarize

The above three sub-topics are just a little bit of my understanding. Thank you for correcting the deficiencies. I will definitely read it! ! !

Guess you like

Origin blog.csdn.net/m0_71214261/article/details/131790163