【 program 1】

subject ： Yes 1,2,3,4 Number , How many different and unrepeated three digit numbers can be composed ? How many are they ?

1. algorithm analysis ： Can be filled in hundreds , Ten , All the numbers are 1,2,3,4. Make up all the permutations and then remove the ones that don't meet the conditions

2. program source code
#include<stdio.h> main() { int i, j, k; printf("\n"); for (i = 1; i < 5;
i++)// The following is a triple cycle for (j = 1; j < 5; j++) for (k = 1; k < 5; k++) { if (i != k && i
!= j && j != k)// ensure i,j,k Three bits are different from each other printf("%d%d%d\n", i, j, k); } getchar(); }
3. Program thinking

a. If you want to calculate the total number of number combinations that meet the conditions , How to modify the source code ?

Program improvement ： Add one int variable count, Used to calculate the number of qualified numbers , The improvement code is as follows
#include<stdio.h> main() { int i, j, k, count = 0; printf("\n"); for (i = 1; i
< 5; i++)// The following is a triple cycle for (j = 1; j < 5; j++) for (k = 1; k < 5; k++) { if (i != k
&& i != j && j != k)// ensure i,j,k Three bits are different from each other { count++;// If conditions are met ,count Self increasing 1
printf("%d%d%d\n", i, j, k); } } printf("count=%d", count);// Print recorded count numerical value
getchar(); }

b. If you want to output the same number of hundreds in rows , How to modify the source code ?

Program improvement ： Add one int variable t, Used to record the first cycle i Value of . Judge after jumping out of the first cycle i-t
Is the value of 1（ That is, whether the number of hundreds increases 1）, if it is 1, Then print 1 Line breaks . The improvement code is as follows
#include<stdio.h> int main() { int i, j, k, t = 1; for (i = 1; i < 5;
i++)// The following is a triple cycle { if (i - t)// If you add hundreds 1 printf("\n");// Print a line break for (j = 1; j < 5;
j++) for (k = 1; k < 5; k++) { if (i != k && i != j && j != k)// ensure i,j,k Three bits are different from each other {
printf("%d%d%d ", i, j, k); } } t = i;// Before recording cycle i Value of } getchar(); }

Technology
Daily Recommendation