3階幻方的一個得到方法 C#

有一個很無聊的問題,就是1-9寫成3*3形式,讓橫豎斜和相等。
今天無聊就改了一個程序,共得到8種結果。
276951438分割
294753618分割
438951276分割
492357816分割
618753294分割
672159834分割
816357492分割
834159672分割

轉載請註明出處,聯繫我: [email protected]
本人熱衷於數據庫技術及算法的研究,志同道合之士, 歡迎探討

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _3階幻方
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private static void PrintArray(int[] array)
        {                  
            for (int i = 0; i < array.Length; i++)
            {                  
                Console.Write(array[i].ToString() + '\t');               
            }
            Console.WriteLine();           
        }

        //判斷一個數組內元素是否降序排列
        private static bool isDesc(int[] array)
        {
            int temp = array[0];
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i] > array[i - 1])
                {
                    return false;
                }
            }
            return true;
        }
        // 找到下一組排列
        private static void FindNextArray(int[] array)
        {
            //1.找出數組的最大值
            int max = array[0];
            for (int i = 1; i < array.Length; i++)
            {
                if (max < array[i])
                {
                    max = array[i];
                }
            }

            //2.從後向前找:找到第一組後數大於前數,以後數位置爲signer
            int signer = array.Length - 1;
            for (int i = array.Length - 1; i > 0; i--)
            {
                if (array[i] > array[i - 1])
                {
                    signer = i;
                    break;
                }
            }

            //3.從signer向後找:找到大於且最接近於array[signer-1]的數array[t]
            int t = signer;
            for (int i = signer; i < array.Length; i++)
            {
                if (array[i] > array[signer - 1] && array[i] < max)
                {
                    t = i;
                    max = array[t];
                }
            }

            //4.將找到的array[t]和array[signer-1]互換
            int temp = array[t];
            array[t] = array[signer - 1];
            array[signer - 1] = temp;

            //5.爲signer之後的元素升序排序
            for (int i = signer; i < array.Length; i++)
            {
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (array[i] > array[j])
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            //1.複製一個新數組:修改時在臨時數組中修改
            int[] temp = new int[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                temp[i] = array[i];
            }

            //2.將新數組升序排列
            int itemp;
            for (int i = 0; i < temp.Length; i++)
            {
                for (int j = i; j < temp.Length; j++)
                {
                    itemp = array[i];
                    array[i] = array[j];
                    array[j] = itemp;
                }
            }

            /*
            for (int i = 0; i < array.Length; i++)
            {
                textBox1.Text = textBox1.Text + temp[i];
            }
            textBox1.Text = textBox1.Text + "分割";
            */

            while (!isDesc(temp))
            {
                FindNextArray(temp);

                if (temp[3] + temp[4] + temp[5] == 15 && temp[0] + temp[1] + temp[2] == 15 && temp[6] + temp[7] + temp[8] == 15 && temp[1] + temp[4] + temp[7] == 15 && temp[0] + temp[3] + temp[6] == 15 && temp[2] + temp[5] + temp[8] == 15 && temp[0] + temp[4] + temp[8] == 15 && temp[2] + temp[4] + temp[6] == 15)
                {
                    for (int i = 0; i < array.Length; i++)
                    {
                        textBox1.Text = textBox1.Text + temp[i];
                    }
                    textBox1.Text = textBox1.Text + "分割";
                }

            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章