這是14題的代碼,和15題一樣,在控制臺中運(yùn)行(排版比較亂,放到編譯器中自動重新排版下吧,沒辦法,回復(fù)答案有字?jǐn)?shù)限制):
int count = 0;/*循環(huán)千位*/
for (int thousand = 1; thousand <= 6; thousand++){/*循環(huán)百位*/
for (int hundred = 1; hundred <= 6; hundred++){/*循環(huán)十位*/
for (int ten = 1; ten <= 6; ten++){/*循環(huán)個位*/
for (int single = 1; single <= 6; single++){
/*判斷千位數(shù)是否和百位數(shù)、十位數(shù)、個位數(shù)相等。*/
bool isThousandEq = thousand != hundred && thousand != ten && thousand != single;
/*判斷百位數(shù)是否和十位數(shù)、個位數(shù)相等*/
bool isHundredEq = hundred != ten && hundred != single;
/*判斷十位數(shù)是否和個位數(shù)相等*/bool isTenEq = ten != single;
if (isThousandEq && isHundredEq && isTenEq){/*計算最終結(jié)果*/
int result = thousand * 1000 + hundred * 100 + ten * 10 + single;
Console.Write(result);Console.Write(",");count++;}}}}}
Console.WriteLine("\n----------------------------------------------------------------");
Console.WriteLine("不重復(fù)的個數(shù):{0}",count);
Console.Read();