C#控制台程序

.Net技术 码拜 8年前 (2016-03-08) 857次浏览
设红、花、映、绿、叶、春都是一个10以内的整数,其中“红花映绿叶”组成一个五位整数。若“红花映绿叶X春=叶绿映花红”,问一下红花映绿叶春分别是什么数。
解决方案

15

答案简直太多了。
笨办法:

int hong,hua,ying,lv,ye,chun = 0;
            string mes = "";
            for (hong = 1; hong < 10; hong++)
            {
                for (hua = 1; hua < 10; hua++)
                {
                    for (ying = 1; ying < 10; ying++)
                    {
                        for (lv = 1; lv < 10; lv++)
                        {
                            for (ye = 1; ye < 10; ye++)
                            {
                                for (chun = 1; chun < 10; chun++)
                                {
                                    int a = hong * 10000 + hua * 1000 + ying * 100 + lv * 10 + ye;
                                    int b = ye * 10000 + lv * 1000 + ying * 100 + hua * 10 + hong;
                                    if (a * chun == b)//红花映绿叶X春=叶绿映花红
                                    {
                                        if (mes != "")
                                            mes += ",红" + hong + ",花" + hua + ",映" + ying + ",绿" + lv + ",叶" + ye + ",春" + chun;
                                        else
                                            mes += "红" + hong + ",花" + hua + ",映" + ying + ",绿" + lv + ",叶" + ye + ",春" + chun;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            MessageBox.Show(mes);

15

            int count = 1;
            string 红 = string.Empty, 花 = string.Empty, 映 = string.Empty, 绿 = string.Empty, 叶 = string.Empty, 春 = string.Empty;
            for (int a = 0; a < 10; a++)
            {
                for (int b = 0; b < 10; b++)
                {
                    for (int c = 0; c < 10; c++)
                    {
                        for (int d = 0; d < 10; d++)
                        {
                            for (int e = 0; e < 10; e++)
                            {
                                for (int f = 0; f < 10; f++)
                                {
                                    红 = a.ToString();
                                    花 = b.ToString();
                                    映 = c.ToString();
                                    绿 = d.ToString();
                                    叶 = e.ToString();
                                    春 = f.ToString();
                                    int result = int.Parse(红 + 花 + 映 + 绿 + 叶) * int.Parse(春);
                                    if (result == int.Parse(叶 + 绿 + 映 + 花 + 红))
                                    {
                                        Console.WriteLine(string.Format("红:{0} 花 :{1} 映 :{2} 绿 :{3} 叶:{4} 春:{5} 计数:{6}", 红, 花, 映, 绿, 叶, 春, count));
                                        count++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Console.Read();

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#控制台程序
喜欢 (0)
[1034331897@qq.com]
分享 (0)