华剑缘
一切都在这个过程中获得,将那些目标埋藏于心中
posts - 19,comments - 20,trackbacks - 0


幻方:

#include  " iostream.h "

int  main()
{
        
int  N,i,j,count;
        cout
<< " Please enter N: " ;
        cin
>> N;
        
int   ** arr = new   int *  [N];
        
for (i = 0 ;i < N;i ++ )
                arr[i]
= new   int [N];
        
for (i = 0 ;i < N;i ++ )
                
for (j = 0 ;j < N;j ++ )
                        arr[i][j]
= 0 ;
        arr[
0 ][(N + 1 ) / 2 - 1 ] = 1 ;
        
for (count = 1 ,i = 0 ,j = (N + 1 ) / 2 - 1 ;count < N * N;count ++ )
        
{
                i
-- ;
                j
-- ;
                
if (i ==- 1 )
                        i
= N - 1 ;
                
if (j ==- 1 )
                        j
= N - 1 ;
                
if (arr[i][j] == 0 )
                        arr[i][j]
= count + 1 ;
                
else   if (arr[i][j] > 0 )
                
{
                        i
= i + 2 ;
                        j
= j + 1 ;
                        
if (j == N)
                                j
= 0 ;
                        
if (i == N)
                                i
= 0 ;
                        
else   if (i == N + 1 )
                                i
= 1 ;
                        arr[i][j]
= count + 1 ;
                }

        }

        
for (i = 0 ;i < N;i ++ )
        
{
                
for (j = 0 ;j < N;j ++ )
                
{
                        cout
<< arr[i][j] << "   " ;
                }

                cout
<< " \n " ;
        }

                     
for (i = 0 ;i < N;i ++ )
                delete [] arr[i];
        delete [] arr;
        
return   1 ;
}
posted on 2006-04-09 22:58 华剑缘 阅读(476) 评论(3)  编辑 收藏 引用

FeedBack:
# re: 幻方,呵呵学习学习
2006-04-10 09:03 | 沐枫
int ** arr = new int * [N];
for (i = 0 ;i < N;i ++ )
arr[i] = new int [N];
for (i = 0 ;i < N;i ++ )
for (j = 0 ;j < N;j ++ )
arr[i][j] = 0 ;

好可怕的数据结构和初始化!!!  回复  更多评论
  
# re: 幻方,呵呵学习学习
2006-04-10 12:27 | 华剑缘
呵呵,我是新手请指教。帮改改好么,不胜感激。  回复  更多评论
  
# re: 幻方,呵呵学习学习
2006-04-14 17:31 | 华剑缘
@沐枫
@沐枫
c:\documents and settings\kakaxi\桌面\新建文件夹\text1.cpp(12) : error C2065: 'for_each' : undeclared identifier
c:\documents and settings\kakaxi\桌面\新建文件夹\text1.cpp(12) : error C2065: 'bind2nd' : undeclared identifier
c:\documents and settings\kakaxi\桌面\新建文件夹\text1.cpp(12) : error C2065: 'mem_fun_ref' : undeclared identifier  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理