C Exercise Example 1

100 Classic C Examples

Title:Yes1、2、3、4With four digits, how many three-digit numbers with distinct and non-repeating digits can be formed? What are they?

Program analysis:The digits that can be placed in the hundreds, tens, and ones places are all1、2、3、4After forming all permutations, remove those that do not meet the conditions.

Example

#include<stdio.h> int main() { int i,j,k; printf("\n"); for(i=1;i<5;i++) { // The following is a triple loop for(j=1;j<5;j++) { for (k=1;k<5;k++) { // Ensure i, j, k are all different if (i!=k&&i!=j&&j!=k) { printf("%d,%d,%d\n",i,j,k); } } } } }

The output of the above example is:

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

100 Classic C Examples

other extensions