zoukankan      html  css  js  c++  java
  • <组合数学>幻方

    我们小时候都玩过数独,数独就是以幻方为基础的游戏。

    行/列的整数和为该幻方的幻和

    我们用s表示幻和。

    对于一个n阶的幻方,幻和

      s = n*(n*n+1) /2

    容易证明,二级幻方不存在。

    三十多年前,有人已证明:n>=3阶的幻方都是存在的。

    循环轮回法构造三阶幻方的模拟:(c语言)

    #include <stdio.h>
    /*
    a simple demo for construction of 3-magic-square
    
    author@DynmiWang
    */
    int arr[3][3];
    int x, y;
    void get_next(int a,int b)
    {
        a--;
        b++;
        if(a<0)
        {
            a += 3;
        }
        if(b<0)
        {
            b += 3;
        }
        a = a % 3;
        b = b % 3;
        if(arr[a][b]!=0)
        {
            a = x;
            b = y;
            a++;
        }   
        x = a;
        y = b;
        if(arr[x][y]!=0)
        {
            get_next(x,y);
        }
    }
    int main()
    {
        printf("请输入构造起点:
    ");
        scanf("%d %d", &x, &y);
        printf(" ##%d %d 
    ", x, y);
        x++;
        y--;
        int p = 1;
        while(p<10)
        {
            get_next(x,y);
            printf(" ##%d %d %d
    ", x, y, p);
            arr[x][y] = p;
            for (int i = 0; i < 3;i++)
            {
                for (int j = 0; j < 3;j++)
                {
                    printf("%d  ", arr[i][j]);
                }
                printf("
    ");
            }
            printf("
    ");
            p++;
        }
        return 0;
    }

    对于奇数阶幻方均可以使用上述轮回反复法;

    另外对于4m阶幻方和4m+2阶幻方各有一种解法。

    在不计重复翻转的情况下,(允许翻转重复的话x8)

    3阶幻方有  1   种排列法;

    4阶幻方有  880  种排列法;

    5阶幻方有  2亿7千多   种排列法

    .......

  • 相关阅读:
    hdoj 2063 过山车
    hdoj 2112 HDU Today
    hdoj 1874 畅通工程续
    hdoj 2544 最短路
    sound of the genuine
    复习webpack的常用loader
    node-sass安装报错
    react-debug
    react-router 4v 路由嵌套问题
    React 中使用sass
  • 原文地址:https://www.cnblogs.com/dynmi/p/12288387.html
Copyright © 2011-2022 走看看