注册 登录
编程论坛 C++教室

填字游戏

aiyinsitan 发布于 2010-04-29 12:41, 1435 次点击
编程完成填字游戏,如下表。
要求:输入一个奇数(n>=3),使该数字表格是奇数行,奇数列,且每行,每列,对角线上数据之和是相等的。
如表:
17   24  1   8   15
23   5   7   14  16
4    6   13  20  22
10  12   19  21  3
11  18   25  2   9
此表用1~25填充后 ,形成5行五列且每行,梅列,对角线上数字之和为65。
  求大侠帮我编写一下
11 回复
#2
aiyinsitan2010-04-29 12:41
先顶一下
#3
wow512010-04-29 12:46
这个题目不知道怎么下手啊 这个路过 卡牛人的解
#4
2010-04-29 17:05
交谈中请勿轻信汇款、中奖信息,勿轻易拨打陌生电话。
我只编到了这里,后面只需要将突出来的数向对面收回去即可,还请其他人继续编一下
例如:
n=3时
输出为  3
      2   6
   1    5    9
      4    8
        7
变成  2  7   6
      9   5   1
      4   3   8
#include<iostream>
#define N 30
using namespace std;
int main()
{
    int n,m,a[N][N],j=1,p,x=0,r;
    for(int w=0;w<N;w++)
        for(int t=0;t<N;t++)
            a[w][t]=' ';
    cin>>n;
    r=N;
    m=r/2;
    p=m;
    for(int b=0;b<n;b++)
    {    for(int i=0;i<=p&&i<n;m--)
        {
            a[m+b][i+b]=j;
            j++;
            i++;
        }
        m=p;
    }
    for(w=0;w<N;w++)
        for(int t=0;t<N;t++)
        {
            if(int(a[w][t])==32)
                cout<<char(a[w][t])<<" ";
            else cout<<a[w][t]<<" ";
            x++;
            if(x%r==0) cout<<endl;
        }
        return 0;
}
#5
Alar302010-04-29 17:11
水平有限
没写出来。。。
#6
温度2010-04-29 17:53
我也在学习中
#7
2010-04-29 19:14
交谈中请勿轻信汇款、中奖信息,勿轻易拨打陌生电话。
我写出来了,只是还有一点缺点,就是输出时32被空格取代了
希望能有人改进一下发出来
#include<iostream>
#include<iomanip>
#define N 30
using namespace std;
int main()
{
    int n,m,a[N][N],j=1,p,x=0,r,c;
    for(int w=0;w<N;w++)
        for(int t=0;t<N;t++)
            a[w][t]=' ';
    cin>>n;
    r=N;
    m=r/2;
    p=m;
    for(int b=0;b<n;b++)
    {    for(int i=0;i<=p&&i<n;m--)
        {
            a[m+b][i+b]=j;
            j++;
            i++;
        }
        m=p;
    }
    for(w=0;w<N;w++)
        for(int t=0;t<N;t++)
        {
            if(int(a[w][t])==32)
                cout<<setw(2)<<char(a[w][t]);
            else cout<<setw(2)<<a[w][t];
            x++;
            if(x%r==0) cout<<endl;
        }
end:for(w=0;w<N;w++)
        for(int t=0;t<N;t++)
        {
            if(a[w][t]==' ')
                continue;
            else if(w<=(n/2-1))
            {
                c=a[w][t];
                a[w][t]=a[w+n][t];
                a[n+w][t]=c;
                goto end;
            }
            else if(t<=(n/2-1))
            {
                c=a[w][t];
                a[w][t]=a[w][t+n];
                a[w][t+n]=c;
                goto end;
            }
            else if(w>=(n+n/2))
            {
                c=a[w][t];
                a[w][t]=a[w-n][t];
                a[w-n][t]=c;
                goto end;
            }
            else if(t>=(n+n/2))
            {
                c=a[w][t];
                a[w][t]=a[w][t-n];
                a[w][t-n]=c;
                goto end;
            }
        }
    for(w=0;w<N;w++)
        for(int t=0;t<N;t++)
        {
            if(a[w][t]==' ')
                cout<<setw(6)<<char(a[w][t]);
            else cout<<setw(6)<<a[w][t];
            x++;
            if(x%r==0) cout<<endl;
        }
        return 0;
}
#8
aiyinsitan2010-04-29 20:18
回复 7楼 clcaogang
好像输入n=9时候
  好像数组中有空格,不过你还是比犀利,我看到是一点思绪多没有
#9
2010-04-29 23:54
#include<iostream>
using namespace std;
const int MAX=50;
int main()
{
      int matrix[MAX][MAX];
      int count;
      int row;
      int column;
      int order;
      cout<<"请输入阶数:";
      cin>>order;
     if(order%2==0)
          {
              cout<<"阶数必须是一个奇数,请重新输入!"<<endl;
          }
        else
            {
              row=0;
              column=order/2;
              for(count=1;count<=order*order;count++)
                  {
                      matrix[row][column] = count;
                  if (count % order == 0)
                      {
                          row++;
                      }
                  else
                      {
                          row = (row == 0) ? order - 1 : row - 1;
                          column = (column == order-1) ? 0 : column + 1;
                      }
                  }
              for (row = 0; row < order; row++)
                  {
                      for (column = 0; column < order; column++)
                          {
                              cout<<"\t"<<matrix[row][column];
                          }
                      cout<<endl;
                  }
              }
        return 0;
          }
#10
2010-04-29 23:59
那个空格就是32 我正在想改进办法
#11
lintaoyn2010-04-30 18:16
记得图书馆里的一本书里也有这么一道题,也有解题的算法……
#12
sunmingchun2010-04-30 18:38
这是魔法阵 我只会奇数的  请教偶数怎么填
#include<stdio.h>
#define N 5
void main()
{
int i,a[N][N],j,t;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
a[i][j]=0;
i=0;
j=N/2;
a[i][j]=1;
for(t=2;t<=N*N;t++)
{
i=i-1;
j=j+1;
if(i<0&&j==N)
{
i=i+2;
j=j-1;
}
else
if(i<0)
i=N-1;
if(j==N)
j=0;
if(a[i][j]==0)
a[i][j]=t;
else
{
i=i+2;
j=j-1;
a[i][j]=t;
}
}
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
    printf(" %d  ",a[i][j]);
    printf("\n");
}
}
你试试吧 我没编译器 随手写的  不知道对不对。

[ 本帖最后由 sunmingchun 于 2010-5-4 20:17 编辑 ]
1