3阶幻方的一个得到方法 C#

有一个很无聊的问题,就是1-9写成3*3形式,让横竖斜和相等。
今天无聊就改了一个程序,共得到8种结果。
276951438分割
294753618分割
438951276分割
492357816分割
618753294分割
672159834分割
816357492分割
834159672分割

转载请注明出处,联系我: t39q@163.com
本人热衷于数据库技术及算法的研究,志同道合之士, 欢迎探讨

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)
        {
            
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值