正文

一道autodesk笔试题的解答2008-01-19 15:43:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/super/32293.html

分享到:

第一题:
实现N*N矩阵,
如N=2时
1 2
4 3
N=4时
1  2  3  4
12 13 14 5
11 16 15 6
10 9  8  7

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

#define N 5
int M[N][N];

void Matrix()
{
 int T = (N+1)/2;
 int num = 0;

 for(int t=0; t<T; t++)
 {
  for(int i=t; i<=N-1-t; i++)
   M[t][i] = ++num;
  for(int i= t+1; i<=N-1-t-1; i++)
   M[i][N-1-t] = ++num;
  for(int i=N-1-t; i>=t; i--)
   M[N-1-t][i] = ++num;
  for(int i=N-1-t-1; i>=t+1; i--)
   M[i][t] = ++num;
 }
 if(N%2 != 0)
  M[N/2][N/2] = N*N;
}

void Print()
{
 for(int i=0; i<=N-1; i++)
 {
  for(int j=0; j<=N-1; j++)
   cout<<setw(5)<<M[i][j];
  cout<<endl;
 }
}

void _tmain()
{
 Matrix();
 Print();
}

阅读(2015) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册