网站上看到这个题目,于是用C#代码将运算结果写了出来,控制台在.NET环境下成功运行。
将代码运行在控制台应用程序中:
static void Main(string[] args)
{
Console.WriteLine("-----------------------------------------------------------------");
Console.WriteLine("由数字1、2、3、4、5、6六个数字共可组成多少个没有重复数字的四位数");
Console.WriteLine(",输出这些数据并统计个数");
Console.WriteLine("----------------------------------------------------------------");
int count = 0;
/*循环千位*/
for (int thousand = 1; thousand <= 6; thousand++)
{
/*循环百位*/
for (int hundred = 1; hundred <= 6; hundred++)
{
/*循环十位*/
for (int ten = 1; ten <= 6; ten++)
{
/*循环个位*/
for (int single = 1; single <= 6; single++)
{
/*判断千位数是否和百位数、十位数、个位数相等。*/
bool isThousandEq = thousand != hundred && thousand != ten && thousand != single;
/*判断百位数是否和十位数、个位数相等*/
bool isHundredEq = hundred != ten && hundred != single;
/*判断十位数是否和个位数相等*/
bool isTenEq = ten != single;
if (isThousandEq && isHundredEq && isTenEq)
{
/*计算最终结果*/
int result = thousand * 1000 + hundred * 100 + ten * 10 + single;
Console.Write(result);
Console.Write(",");
count++;
}
}
}
}
}
Console.WriteLine("/n----------------------------------------------------------------");
Console.WriteLine("不重复的个数:{0}",count);
Console.Read();
}
输出结果如下:
最终结果是不重复的个数是360个
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/98324.html