Wonderful Example 1 of C language: There are 1, 2, 3, and 4 numbers. How many three-digit numbers can be formed that are different from each other and have no repeated numbers? How many are they?

1. Question

There are 1, 2, 3, and 4 numbers. How many different three-digit numbers can be formed without repeated numbers? How many are they?

2. Program analysis

The numbers that can be filled in the hundreds, tens, and ones digits are 1, 2, 3, and 4. After composing all the permutations, remove the permutations that do not meet the conditions.

3. Program source code:

#include<stdio.h>
 
int main()
{
    int i,j,k;
    printf("\n");
    for(i=1;i<5;i++) { // 以下为三重循环
        for(j=1;j<5;j++) {
            for (k=1;k<5;k++) { // 确保i、j、k三位互不相同
                if (i!=k&&i!=j&&j!=k) { 
                    printf("%d,%d,%d\n",i,j,k);
                }
            }
        }
    }
}

4.Input and output

Standard output:

Standard output:
1,2,3
1,2,4
1,3,2
1,3,4
1,4,2
1,4,3
2,1,3
2,1,4
2,3,1
2,3,4
2,4,1
2,4,3
3,1,2
3,1,4
3,2,1
3,2,4
3,4,1
3,4,2
4,1,2
4,1,3
4,2,1
4,2,3
4,3,1
4,3,2

 

Guess you like

Origin blog.csdn.net/T19900/article/details/129506107