| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 215 人关注过本帖
标题:求奇数阶幻方 指点错误
只看楼主 加入收藏
近天门梦易回
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-11-12
结帖率:0
收藏
已结贴  问题点数:10 回复次数:3 
求奇数阶幻方 指点错误
#include<stdio.h>
#define N 7
void main(){
    int a[N][N] = {}, i, j, n, m=0;
    printf("%d阶幻方如下:\n", N);
    a[0][(N - 1) / 2] = 1;
    for (n = 2, i = 0, j = (N - 1) / 2; n <= N*N; n++){
        if (i - 1 >= 0 && j + 1 < N){
            if (a[i - 1][j + 1] == 0){
                a[i - 1][j + 1] = n;
                i--;
                j++;
                break;
            }
            else{
                a[i][j + 1] = n;
                j++;
                break;
            }
        }
         if (i - 1 < 0&&j+1<N){
            for (i = N - 1; i >= 0; i--){
                if (a[i][j+1] == 0){
                    a[i][j+1] = n;
                    j++;
                    break;
                }
            }
        }
         if (i - 1 >= 0 && j + 1 >= N){
            for (j = 0; j < N; j++){
                if (a[i-1][j] == 0){
                    a[i-1][j] = n;
                    i--;
                    break;
                }
            }
        }
     if (i == 0 && j == N - 1){
            i++;
            a[i][j] = n;
            break;
        }
    }
    for (i=0;i<N; i++){
        for (j = 0; j < N; j++){
            printf("%4d\t",a[i][j] );
            m++;
            if (m%N == 0)
                printf("\n");
        }
    }
}
搜索更多相关主题的帖子: include 
2014-11-12 20:45
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:10 
你先把所有的break都改成continue

重剑无锋,大巧不工
2014-11-12 21:27
近天门梦易回
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-11-12
收藏
得分:0 
谢谢,已经搞定了,可以看看还有啥改进的么
#include<stdio.h>
#define N 7
void main(){
    int a[N][N] = {0}, i, j, n, m=0;
    printf("%d阶幻方如下:\n", N);
    a[0][(N - 1) / 2] = 1;
    for (n = 2, i = 0, j = (N - 1) / 2; n <= N*N; n++){
        if (i - 1 >= 0 && j + 1 < N){
            if (a[i - 1][j + 1] == 0){
                i--;
                j++;
            }
            else{
                a[i+1][j] = n;
                i++;
            }
        }
        else if (i - 1 < 0&&j+1<N){
                if (a[N-1][j+1] == 0){
                    j++;
                    i = N-1;
                }
                else{
                    i++;
                }
        }
        else if (i - 1 >= 0 && j + 1 >= N){
                if (a[i-1][0] == 0){
                    i--;
                    j = 0;
                }
                else{
                    j = 0;
                }
        }
        else if (i == 0 && j == N - 1){
            i++;
        }
         a[i][j] = n;
    }
    for (i=0;i<N; i++){
        for (j = 0; j < N; j++){
            printf("%4d\t",a[i][j] );
            m++;
            if (m%N == 0)
                printf("\n");
        }
    }
}
2014-11-13 19:20
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <malloc.h>

void output_magic(int n)
{
    int * a, i, j, k;
    if(!(n & 1)) return;
    a = (int *)calloc(n * n, sizeof(int));
    for(i = 1, j = 0, k = n / 2; i <= n * n; i++)
    {
        a[j * n + k] = i;
        j = j ? j - 1 : n - 1;
        k = (k + 1) % n;
        if(a[j * n + k])
            k = k ? k - 1 : n - 1,
            j = (j + 2) % n;
    }
    for(i = 0; i < n; i++, puts(""))
    for(j = 0; j < n; printf("%3d ", a[i * n + j++]));
    free(a);
}

int main()
{
    int n;
    scanf("%d", &n);
    output_magic(n);
    return 0;
}

重剑无锋,大巧不工
2014-11-13 20:54
快速回复:求奇数阶幻方 指点错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017379 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved