Example 1-2 three-digit reversal
Enter a three-digit number, separating out its hundred, 10-bit and single-digit, inverted output.
Sample input:
127
Sample output:
721
Program 1-6 three digit reversal (1)
1#include <stdio.h>2 intMain ()3 {4 intN;5scanf"%d", &n);6printf"%d%d%d"NTenN -/Ten, n/ -);7 8 return 0;9}
When the input is a normal three-digit number, you can achieve the purpose.
However, if the input is a number ending in 0, the reversal will be 0. The question is not clear. If you want to output something like 052, you need to make the following changes:
Program 1-7 three digit reversal (2)
1#include <stdio.h>2 intMain ()3 {4 intN;5scanf"%d", &n);6 intM//New7m = (n%Ten) * -+ (N/Ten) %Ten*Ten+ (N/ -);//Modify8printf"%03d", m);9 Ten return 0; One}
Note here that line 8th, 3 means that less than three bits of the output with three digits, the front is empty, and 03 means less than three bits of the three-bit output is preceded by 0 padding. It's easy to understand, but it's a good trick.
Example 1-3 swap variables
Enter two integers a and b, swap the values of both, and then output.
Sample input:
824 16
Sample output:
16 824
Program 1-8 Swap variables (1)
#include <stdio.h>int main () { int A, b; scanf ("%d%d", &a, &b); int temp; = A; = B; = temp; printf ("%d%d", A, b);}
The first method is easiest to think of, introducing a third-party variable to temporarily store one of the values. Can be likened to white vinegar and soy sauce with a third empty bottle swap. But there are some differences, when the soy sauce is poured into an empty bottle, the original soy sauce bottle is empty. in the computer language, the value of a is changed after the assignment of a = B, but the value of B does not change.
The second method does not use the third variable:
Program 1-9 variable Exchange (2)
#include <stdio.h>int main () { int A, b; scanf ("%d%d", &a, &b); = a + b; = A- b; = A- b; printf ("%d%d", A, b);}
Step by step down to understand, but still not the first one good. It is recommended to use the first method in practice:
Program 1-10 variable Exchange (3)
1 #include <stdio.h>2int main ()3{4 int A, b; 5 scanf ("%d%d", &a, &b); 6 printf ("%d%d", B, a); 7 }
The third method compares the flattering, uses not to see the concrete situation.
1.3 Sequential Structure programming