当前位置:编程学习 > C#/ASP.NET >>

排列问题

用1,2,3...9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3。输出所以解,希望吓哥们能尽快给出解答 
--------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class CSDN : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Response.Write(IsOneTwoThree(1, 2, 3));
            OutputResult();
        }
    }

    public void OutputResult()
    {
        ArrayList al = new ArrayList() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        ArrayList al1 = new ArrayList();
        ArrayList al2 = new ArrayList();
        ArrayList al3 = new ArrayList();
        ArrayList al4 = new ArrayList();
        ArrayList al5 = new ArrayList();
        ArrayList al6 = new ArrayList();
        ArrayList al7 = new ArrayList();
        ArrayList al8 = new ArrayList();
        int count = 0;
        for (int i1 = 1; i1 <= 9; i1++)
        {
            al1 = new ArrayList(al.Count) { al[0], al[1], al[2], al[3], al[4], al[5], al[6], al[7], al[8] };
            al1.RemoveAt(al1.IndexOf(al1[i1 - 1]));
            for (int i2 = 1; i2 <= 8; i2++)
            {
                al2 = new ArrayList(al1.Count) { al1[0], al1[1], al1[2], al1[3], al1[4], al1[5], al1[6], al1[7] };
                al2.RemoveAt(al2.IndexOf(al2[i2 - 1]));
                for (int i3 = 1; i3 <= 7; i3++)
                {
                    al3 = new ArrayList(al2.Count) { al2[0], al2[1], al2[2], al2[3], al2[4], al2[5], al2[6] };
                    al3.RemoveAt(al3.IndexOf(al3[i3 - 1]));
                    for (int i4 = 1; i4 <= 6; i4++)
                    {
                        al4 = new ArrayList(al3.Count) { al3[0], al3[1], al3[2], al3[3], al3[4], al3[5] };
                        al4.RemoveAt(al4.IndexOf(al4[i4 - 1]));
                        for (int i5 = 1; i5 <= 5; i5++)
                        {
                            al5 = new ArrayList(al4.Count) { al4[0], al4[1], al4[2], al4[3], al4[4] };
                            al5.RemoveAt(al5.IndexOf(al5[i5 - 1]));
                            for (int i6 = 1; i6 <= 4; i6++)
                            {
                                al6 = new ArrayList(al5.Count) { al5[0], al5[1], al5[2], al5[3] };
                                al6.RemoveAt(al6.IndexOf(al6[i6 - 1]));
                                for (int i7 = 1; i7 <= 3; i7++)
                                {
                                    al7 = new ArrayList(al6.Count) { al6[0], al6[1], al6[2] };
                                    al7.RemoveAt(al7.IndexOf(al7[i7 - 1]));
                                    count += 1;
                                    //Response.Write(string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8} 当前第{9}个", al[i1 - 1], al1[i2 - 1], al2[i3 - 1], al3[i4 - 1], al4[i5 - 1], al5[i6 - 1], al6[i7 - 1], al7[0], al7[1], count) + "</br>");
                                    long s = long.Parse("" + al[i1 - 1] + al1[i2 - 1] + al2[i3 - 1] + al3[i4 - 1] + al4[i5 - 1] + al5[i6 - 1] + al6[i7 - 1] + al7[0] + al7[1]);
                                    long a = s / 100000;
                                    long b = (s - a * 100000) / 100;
                                    long c = s - a * 100000 - b * 100;
                                    if (IsOneTwoThree(a, b, c))
                                    {
                                        Response.Write(string.Format("{0}:{1}:{2}", a, b, c));
                                        Response.Write("</br>");
                                    }
                                    count += 1;
                                    //Response.Write(string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8} 当前第{9}个", al[i1 - 1], al1[i2 - 1], al2[i3 - 1], al3[i4 - 1], al4[i5 - 1], al5[i6 - 1], al6[i7 - 1], al7[1], al7[0], count) + "</br>");
                                    long s1 = long.Parse("" + al[i1 - 1] + al1[i2 - 1] + al2[i3 - 1] + al3[i4 - 1] + al4[i5 - 1] + al5[i6 - 1] + al6[i7 - 1] + al7[1] + al7[0]);
                                    long a1 = s / 10000;
                                    long b1 = (s - a * 100000) / 100;
                                    long c1 = s - a * 100000 - b * 100;
                                    if (IsOneTwoThree(a1, b1, c1))
                                    {
                                        Response.Write(string.Format("{0}:{1}:{2}", a1, b1, c1));
                                        Response.Write("</br>");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        Response.Write("" + count);
    }

    public bool IsOneTwoThree(long a, long b, long c)
    {
        bool flag = false;
        if ((b / a) == 2 && (c / a) == 3&&(a/b)==2/3)
        {
            flag = true;
        }
        return flag;
    }
}


貌似还有点小问题,等回家明天再改改看
补充:.NET技术 ,  .NET技术前瞻
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,