General remarks
quite ingenious ideas are worth learning for me as a beginner.
in short, I see less code, think less and write less. These codes give people a refreshing feeling.
I will learn and supplement slowly.
1, About reverse output digital
1) 4-digit reverse output
-
Similar questions: output binary digits, octal digits and hexadecimal digits
-
Method I:
a writing method that the author needs to learn.
this writing method strongly calls the thinking of "divide by 10 and more than 10" to complete the digital conversion process.
#define _CRT_SECURE_NO_WARNINGS / / preprocessing command and identifier #include <stdio.h> int main(void) { //Enter a four digit integer: int a; printf("Enter four digits:"); scanf("%d", &a); //Performs a loop on the integer to reverse the digits. int remοΌi; for (i = 0; i < 4; i++) { rem=a% 10; a /= 10; printf("%d", rem); } return 0; }
the above code can be further optimized. In addition to using the for loop, you can also consider the while loop, etc.
as follows: the advantage of writing this way is that it reduces the use of variables, is relatively concise and has strong universality.
#define _CRT_SECURE_NO_WARNINGS / / preprocessing command and identifier #include <stdio.h> int main(void) { int a; printf("Enter four digits:"); scanf("%d", &a); while(a) { printf("%d", a % 10); a /= 10; } return 0; }
ββ
- Adaptation of law I
the method is logically optimized again and again to make it more in line with the mathematical operation rules.
ps: adding a definition function is purely an exercise, which can also be completed directly in the main function. However, considering that when the program is complicated, not all the code is thrown into the main function, so the method of defining the function is adopted
#define _CRT_SECURE_NO_WARNINGS / / preprocessing command and identifier #include <stdio.h> //Create a function that inputs a positive integer. int post(void) { printf("Enter a positive integer:")οΌ scanf("%d", &num); return num; } //Create a function that evaluates inverse values. int rev(int num) { int i = 0; do{ i = i*10 + num %10; num /= 10; }while(num); return num; } int main(void) { printf("The reverse value is:%d",rev(post())); return 0; }
- Method II
a more magical writing method (I thought about this writing method, but I changed my thinking when doing the question considering that the code can't only stay at a relatively simple level)
#define _CRT_SECURE_NO_WARNINGS / / preprocessing command and identifier #include <stdio.h> int main(void) { int a, b, c, d; printf("Enter four digits:"); scanf("%1d%1d%1d%1d", &a, &b, &c, &d); printf("%d%d%d%d",d,c,b,a); return 0; }
ββ
- Method three
Insert the code slice here
ββ
ββ
ββ
2, About multiples
1) . output multiple of 3 between 0-100
- Analogy question: output odd and even numbers
- Method I:
#include <stdio.h> int main(void) { int i; int count=0; for (i = 1; i < 100; i++) { if (0 == i % 3) { printf("%d ", i); count++; } } printf("\n common%d individual\n", count); return 0; } //3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 //54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 //33 in total
optimize method 1. For the multiple of 3, each subsequent item is three times larger than the previous one, so we can get:
#include<stdio.h> int main() { int i; int count = 0; for(i = 3; i < 100 ; i +=3) { printf("%d ", i); count++; } printf("\n share%d Multiple of 3\n",count); return 0; }
ββ
ββ
3, Find the greatest common divisor
- Basic introduction
common divisor, i.e. "common factor", refers to a number that can divide several integers at the same time. The greatest factor among these numbers is the greatest common divisor.
ββ - Method 1: Trial Division
trial division is a relatively simple method for finding the maximum common divisor. The idea is as follows:
for two numbers A and B, if A is A multiple of B, the largest common divisor of the two numbers is B (the smallest common multiple is A). In other words, the greatest common divisor cannot exceed the smallest of A and B. In that case, if A > b, you just need to start from B and decrease accordingly to find the number that can divide A and B at the same time. The first number found is the maximum common divisor.
expressed by code as follows:
#define _CRT_SECURE_NO_WARNINGS / / preprocessing command and identifier #include<stdio.h> int main () { int n1 , n2; printf("Enter two integers:")οΌ scanf("%d, %d", &n1, &n2); //Find the smaller of the two numbers. int min = 0; if(n1 > n2) min = n2; else min = n1; //Find the greatest common divisor. while(1) { if( n1 % min == 0 && n2 % min == 0) break; min--; } //It should be noted here that if the two numbers are prime numbers, the maximum common divisor of the two is 1 (the minimum common multiple is the product of the two). //Therefore, min is finally decremented to 1. If the if statement is satisfied, it can also jump out of the loop. printf("The maximum common divisor is:%d",min); return 0; }
ββ
- Method 2: rolling Division
Rolling division is a common method in mathematics.
expressed by code as follows:
#define _CRT_SECURE_NO_WARNINGS / / preprocessing command and identifier #include<stdio.h> int main () { int a , b; printf("Enter two integers:"); scanf("%d %d", &a, &b); int exc = 1; while (exc != 0 ) { exc = a % b; a = b; b = exc; } printf("The maximum common divisor is:%d", a); return 0;