Unknowingly, it was 2022. I felt that I was too delayed during the holiday, so I knocked the code during the online class_ (: з」 ∠)_
Catalogue
1. Early stage code: output prime numbers within 100 ~ 200 ----- > function realization + sublimation (judge whether the input is prime).
2. Early stage code: output leap year from 1000 to 2000 ----- > function realization + sublimation (judge whether the input is leap year)
3. Early stage code: it is realized by using the subscript ----- > function corresponding to the number in the ordered array
When we learn functions in programming language, we can't help feeling how pleasant it is to establish a pure function. (the function should be short and concise. The purer the function, the better). Reasonable use of functions can reduce the main () function is too complicated. Make it easier for others to read your code.
Prime number ------------- prime number
Leap year
binary search
1. 100 ~ 200 prime numbers.
Previous code:
#include<math.h> #include<stdio.h> int main() { int i = 0; int j = 0; int count = 0; for (i = 100; i <= 200; i++) { int flag = 1; for (j = 2; j <= sqrt(i); j++) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) { count++; printf("%d ", i); } } printf("\ncount = %d", count); return 0; }
In this code, we use the properties of prime numbers (only prime numbers are prime numbers when they can be divided by 1 , and , itself). After the two-layer loop generates a new i, we use j to try division, and use the ingenious properties of flag to tell us that we have found prime numbers. What we try to divide is the unsatisfied condition. if we can't use else because else contains the case that the modulus is not equal to. The number that can't be judged by j is prime.
Function implementation:
#include<math.h> int is_prime(int n) { int j = 0; for (j = 2; j <= sqrt(n); j++) { if (n % j == 0) { return 0; } } return 1; } #include<stdio.h> int main() { int i = 0; int count = 0; for (i = 100; i <= 200; i++) { if (is_prime(i) == 1) { count++; printf("%d ", i); } } printf("\ncount = %d", count); return 0; }
The principle of function implementation is almost the same, more from main function to is function_ The parameter transfer process of prime. At {is_ The function of return 0 in the prime # function is to screen out non prime numbers and jump out of the loop (since the loop can be used to screen out non prime numbers, the prime number cannot be recognized after the loop, and the loop will jump out and return the value # 1 # to the main function). We don't have to worry about whether the value of # i # changes in this process, because a special address is opened in the memory to put the value of # i # without transmitting the right value to the left value, i # cannot be changed, so the cycle is continuous.
Sublimation:
#include<math.h> int is_prime(int a) { int j = 0; for (j = 2; j <= sqrt(a); j++) { if (a % j == 0) { return 0; } } return 1; } #include<stdio.h> int main() { int a = 0; scanf("%d", &a); if (is_prime(a) == 1) printf("%d It's a prime\n", a); else printf("%d Not prime\n", a); }
It can be seen that judging whether an input number is a prime number is an element to find the prime number in the range. It can be found with only one layer of circulation. We need to note that the condition of sqrt (a) is < =. Simply using the less than sign will lead to judgment 121, 4, etc. being even.
2.1000 ~ 2000 leap years
Previous code:
#include<stdio.h> int main() { int i = 0; int count = 0; for (i = 1000; i <= 2000; i++) { if ((i % 4 == 0) && (i % 100 != 0) || (i % 400 == 0)) { count++; printf("%d ", i); } } printf("\ncount = %d", count); return 0; }
It's easy to judge leap years. Just remember the conditions.
Function implementation:
int is_leap_year(int i) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { return 1; } else return 0; } #include<stdio.h> int main() { int count = 0; int y = 0; for (y = 1000; y <= 2000; y++) { if (is_leap_year(y) == 1) { count++; printf("%d ", y); } } printf("\ncount = %d", count); return 0; }
is_ leap_ Leap years are printed only when year() returns 1, excluding non leap years.
Sublimation:
int is_leap_year(int i) { if ((i % 4 == 0) && (i % 100 != 0) || (i % 400 == 0)) return 1; else { return 0; } } #include<stdio.h> int main() { int y = 0; scanf("%d", &y); if (is_leap_year(y) == 1) printf("%d It's a leap year", y); else printf("%d Not a leap year", y); return 0; }
This is also a problem within the scope, which becomes one of the elements.
3. Binary search ordered array.
Previous code:
#include<stdio.h> int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; //Digits 0 1 2 3 4 5 6 7 8 9 int n = 0; scanf("%d", &n); int sz = sizeof(arr) / sizeof(arr[1]); //Establish left and right Subscripts int left = 0; int right = sz - 1; while (left <= right) { int mid = (left + right) / 2; if (arr[mid] < n) { left = mid + 1; } else if (arr[mid] > n) { right = mid - 1; } else { printf("eureka,Subscript %d", mid); break; } } return 0; }
Function implementation:
int binary_search(int a, int arr[], int sz) { int left = 0; int right = sz - 1; while (left <= right) { int mid = (right + left) / 2; if (arr[mid] > a) right = mid - 1; else if (arr[mid] < a) left = mid + 1; else return mid; } if (left > right) { return -1; } } #include<stdio.h> int main() { int n = 0; scanf("%d", &n); int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; int sz = sizeof(arr) / sizeof(arr[1]); int j = binary_search(n, arr, sz); if (j == -1) { printf("can't find" ); } else printf("Found, subscript:%d",j); return 0; }
The binary search here is very ingenious, because the function is used to judge, and the search is not equal to a simple judgment (for example, if the condition is met, it is a prime number, it is a leap year), so we need to use the function to return the subscript, and the subscript is an integer of 0~n, which can only return what is found. If it cannot be found, it can return a negative number or decimal, which is obviously simpler.
Finally, after practice, eat a bowl of duck blood vermicelli (o(* ~) ~ *) o)