The first exercise is to calculate factorials:
int main() { int i = 0; int ret = 1; int n = 0; while (1) { scanf("%d", &n); if (0 == n) { printf("Exit program\n"); break; } for (i = 1,ret = 1; i <= n; i++) { ret *= i; } printf("%d The factorial of is:%d\n", n, ret); } return 0; }
Compared with the classroom, i made some changes. i think it's too troublesome to reopen every factorial input. Just make a cycle. If you want to exit, enter 0 directly; Code explanation, factorial, you all know, 3= one × two × 3; 4!= one × two × three × 4; And so on... And here we can see that the factorial of 4 and the factorial of 3 overlap; Here, take the RET variable as the factorial value and i as the value to be multiplied. Assign a value to ret every time the product is executed. After you understand the for loop statement, you can compile it in your mind. i won't talk about it in detail here. It's very understandable;
The second exercise is factorial and:
int main() { int i = 0; int n = 0; int ret = 1; int sum = 0; while (1) { scanf("%d", &n); if (0 == n) { printf("Exit program\n"); break; } ret = 1; sum = 0; for (i = 1; i <= n; i++) { ret *= i; sum += ret; } printf("%d Factorial sum within:%d\n", n, sum); } return 0; }
Factorial sum is to make a change in the factorial code. Add up the factorial values each time. At the beginning, I directly clicked a nested one. As a result, I found that it is easier to write, and there are people outside ~;
The third exercise is to find values in an ordered array, using half search, also known as binary search
int main() { int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; int k = 7; int sz = sizeof(arr) / sizeof(arr[0]); //Find the number of array elements int left = 0; //Range left subscript int right = sz - 1; //Range right subscript int mid = 0; while (left <= right) { mid = (left + right) / 2; //Find from the middle of the array if (arr[mid] > k) right = mid - 1; else if (arr[mid] < k) left = mid + 1; //Narrow the search else { printf("eureka,The subscript is:%d\n", mid); break; } } if (left > right) printf("Can not find\n"); //When the left subscript of the range is greater than the right subscript, it means that there are no elements to find return 0; }
Find the required elements in the ordered array. Half search literally means to start from the middle. Half of the elements will be removed in each search. left and right represent the search range, which is determined by the number of array elements;
In the while loop, the left subscript < = right subscript is used. The array is arranged in the order from small to large. Each search is compared with the middle subscript. When left > right, it is proved that there are no elements to be searched. Therefore, the judgment condition we set for the while loop is that there are still elements to be searched in the array. If not, we jump out of the loop;
Then, in the following search process, each time the intermediate element is compared with the search value to narrow the range according to the situation; The comparison is nothing more than three ">, <, =". When found, print the subscript and jump out of the loop; When we jump out of the loop and can't find it, it means that the left subscript is greater than the right subscript, and all that needs to be found have been found, which means that there is no value we want to find in the array, so an if statement is used outside the loop to set this condition;
The fourth exercise is a code displayed one by one from both ends to the middle;
int main() { char arr1[] = "Nice to meet you!"; char arr2[] = "#################"; int left = 0; int right = strlen(arr1) - 1; while (left <= right) { arr2[left] = arr1[left]; arr2[right] = arr1[right]; printf("%s\n", arr2); Sleep(1000); system("cls"); left++; right--; } printf("%s\n", arr2); return 0; }
This code is to correspond the elements at both ends of the second string to the first string one by one; Until printing to the middle, the judgment condition of this cycle is the same as that of the binary search above. Left < = right indicates that there are still contents to be printed. When left > right, you don't need to continue printing. Here we learned two commands, one sleep and the other system command "cls"; Sleep is to let the code output wait for a period of time before continuing to execute the following code. cls is to clear the screen output content; It's cool.
Fifth, simulate the code that limits the number of password entries:
int main() { int i = 0; char password[20] = { 0 }; for(i = 0;i <3;i++) { printf("Please input a password:>"); scanf("%s", password); if (strcmp(password, "123456") == 0) { printf("Login succeeded\n"); break; } else printf("Input error, please re-enter\n"); } if (i == 3) printf("Input error three times, exit the program\n"); return 0; }
Here we need to talk about the keyword "strcmp" used for comparison between two strings; When we don't know this keyword, we still want to compare the two strings in the way of equal sign. However, the result is obviously easy to see, and we can't realize it because we learn too little; This strcmp keyword is used to compare strings. When two strings are the same, it returns 0. When they are different, there are two cases. strcmp comparison strings are compared based on the ASCII code value of the string. Here, arr is used to represent the string. When the ASCII code value of arr1 is greater than arr2, the return value is > 1, otherwise < 1; Generally speaking, the return value will be expressed in the form of - 1, 0 and - 1;
These are some exercises I have learned. Please give me more advice;