菜鸟!求一题目答案?
1-9九位数,填空。四位数乘以一位数,等于四位数。那九个数字不能重复使用 这个怎么做啊?用for语句对吗?
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int jishu = 1234,i1, i2, i3, i4, xx; for (; jishu <= 9876; jishu++) { //jishu = i4 i3 i2 i1; //取得每位的数字值 i1 = jishu % 10; i2 = jishu / 10 % 10; i3 = jishu / 100 % 10; i4 = jishu / 1000 % 10; if (i1 == i2 || i1 == i3 || i1 == i4 || i2 == i3 || i3 == i4) //如果有相同的数字,则继续判断 continue; for (xx = 1; xx < 9; xx++) { if (xx == i1 || xx == i2 || xx == i3 || xx == i4) //如果一位数与四位数相同,则不参与 计算 continue; if (xx * jishu < 10000) { int result = xx * jishu; int ji1,ji2,ji3,ji4; //取得计算结果的每位数值 ji1 = result % 10; ji2 = result / 10 % 10; ji3 = result / 100 % 10; ji4 = result / 1000 % 10; if (ji1 == ji2 || ji1== ji3 || ji1==ji4 || ji2==ji3|| ji2==ji4 ||ji3 ==ji4 ) //如果计算出的数字相同,则继续判断 continue; if (i1 == ji1 || i1 == ji2 || i1 == ji3 || i1 == ji4 || i2 == ji1 || i2 == ji2 || i2 == ji3 || i2 == ji4 || i3 == ji1 || i3 == ji2 || i3 == ji3 || i3 == ji4 || i4 == ji1 || i4 == ji2 || i4 == ji3 || i4 == ji4 || xx == ji1 || xx == ji2 || xx == ji3 || xx == ji4) //判断计算中所用的五个数字是否是相同的。 continue; Console.WriteLine("{0}*{1}={2}", jishu, xx,result); } } } Console.ReadKey(); } } } 计算结果 1278*5=6390 1309*6=7854 1315*6=7890 1317*4=5268 1319*4=5276 1384*5=6920 1417*6=8502 1513*6=9078 1518*4=6072 1548*2=3096 1609*3=4827 1619*3=4857 1694*3=5082 1713*4=6852 1738*4=6952 1809*3=5427 1809*4=7236 1815*4=7260 1845*2=3690 1846*5=9230 1864*5=9320 1872*5=9360 1908*3=5724 1908*4=7632 1913*4=7652 1916*3=5748 1963*4=7852 2039*4=8156 2058*3=6174 2079*4=8316 3079*2=6158 3435*2=6870 3485*2=6970 3534*2=7068 3548*2=7096 3845*2=7690 4345*2=8690 4518*2=9036 4538*2=9076 4543*2=9086 4685*2=9370 4815*2=9630 4835*2=9670 4853*2=9706 4865*2=9730
static void Main(string[] args) { int jishu = 1234, i1, i2, i3, i4, xx; for (; jishu <= 9876; jishu++) { //jishu = i4 i3 i2 i1; //取得每位的数字值 i1 = jishu % 10; i2 = jishu / 10 % 10; i3 = jishu / 100 % 10; i4 = jishu / 1000; if (i1 == i2 || i1 == i3 || i1 == i4 || i2 == i3|| i2 == i4 || i3 == i4|| i1 == 0 || i2 == 0 || i3 == 0) //如果有相同的数字,则继续判断 continue; for (xx = 1; xx < 9; xx++) { if (xx == i1 || xx == i2 || xx == i3 || xx == i4) //如果一位数与四位数相同,则不参与 计算 continue; if (xx * jishu < 10000) { int result = xx * jishu; int ji1, ji2, ji3, ji4; //取得计算结果的每位数值 ji1 = result % 10; ji2 = result / 10 % 10; ji3 = result / 100 % 10; ji4 = result / 1000; if (ji1 == ji2 || ji1 == ji3 || ji1 == ji4 || ji2 == ji3 || ji2 == ji4 || ji3 == ji4 || ji1 == 0 || ji2 == 0 || ji3 == 0) //如果计算出的数字相同,则继续判断 continue; if (i1 == ji1 || i1 == ji2 || i1 == ji3 || i1 == ji4 || i2 == ji1 || i2 == ji2 || i2 == ji3 || i2 == ji4 || i3 == ji1 || i3 == ji2 || i3 == ji3 || i3 == ji4 || i4 == ji1 || i4 == ji2 || i4 == ji3 || i4 == ji4 || xx == ji1 || xx == ji2 || xx == ji3 || xx == ji4) //判断计算中所用的五个数字是否是相同的。 continue; Console.WriteLine("{0}*{1}={2}", jishu, xx, result); } } } Console.ReadKey(); }