题目:有 1、2、3、4 四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
程序分析:可填在百位、十位、个位的数字都是 1、2、3、4,组成所有的排列后再去掉不满足条件的排列。
#include <stdio.h>
int main()
{
int hundreds, tens, units;
int count = 0;
// 遍历百位可能的数字
for (hundreds = 1; hundreds <= 4;hundreds++)
{
// 遍历十位可能的数字
for (tens = 1;tens <= 4; tens++)
{
if (tens == hundreds)
{
continue;
}
for (units = 1; units <= 4; units++)
{
if (units == hundreds || units == tens)
{
continue;
}
printf("%d%d%d ", hundreds, tens, units);
count++;
if (count % 5 == 0)
printf("\n");
}
}
}
// 输出总数
printf("\n总共有%d个互不相同且无重复数字的三位数\n",count);
return 0;
}
本网站原创文章版权归何大锤的狂飙日记所有。发布者:何大锤,转转请注明出处:何大锤的博客
评论列表(1条)
1