Lanffulce 的个人资料Lanffulce G照片日志列表更多 工具 帮助
1月15日

一个关于C++链表的最初型

#include <iostream.h>
#include <stdio.h>

int i_Input =0; //阵的行列
int i_CurrentRow =0; //所在行,后边有设定
int i_CurrentColumn =0; //所在列,后边有设定

struct Cell
{
 int i_Row; //排序用
 int i_Column; //排序用
 long l_Number; //实际填入的数值
 Cell *next;
};


void RC_move(long l_Currenti)
{
 if((l_Currenti - 1) % i_Input ==0)
 {
  (i_CurrentRow ==i_Input - 1)? i_CurrentRow =0 : i_CurrentRow++;
 }
 else
 {
  (i_CurrentRow ==0)? i_CurrentRow =i_Input - 1 : i_CurrentRow--;
  (i_CurrentColumn ==i_Input - 1)? i_CurrentColumn =0 : i_CurrentColumn++;
 }
};

void main()
{
 //cout <<"解决一个数学问题:在方阵内填数,是横行、数列的和相等" <<endl;
 //cout <<"请输入您准备建立的方阵行或列的元素个数(奇数):";
 cout <<"Please input the number of row or column:";
 cin >>i_Input;
 

 Cell *head; //链首指针
 Cell *p_CurrentCell;
 Cell *p_End;
 p_CurrentCell =new Cell;
 p_CurrentCell ->i_Column =-1;
 p_CurrentCell ->i_Row =-1;
 p_CurrentCell ->l_Number =0;
 head =NULL;
 p_End =p_CurrentCell;

 //此处可设定开始时的1的所在位置
 i_CurrentRow =0; //所在行
 i_CurrentColumn =0; //所在列

 for(long i=1; i<=i_Input * i_Input+1; i++)
 {
  if(head == NULL)
  {
   head =p_CurrentCell;
  }
  else
  {
   p_End  ->next =p_CurrentCell;
  }
  p_End =p_CurrentCell;
  p_CurrentCell =new Cell;
  RC_move(i);
  p_CurrentCell ->i_Row =i_CurrentRow;
  p_CurrentCell ->i_Column =i_CurrentColumn;
  p_CurrentCell ->l_Number =i;
 }
 p_End ->next =NULL;
 delete p_CurrentCell;

 Cell *head_result;
 for(int j =0; j < i_Input; j++)
 {
  for(int k =0; k <i_Input; k++)
  {
   head_result =new Cell;
   head_result =head;
   while(head_result ->l_Number < i_Input * i_Input)
   {
    head_result =head_result ->next;
    if(head_result ->i_Row == j && head_result ->i_Column == k)
    {
     cout <<head_result ->l_Number <<'\t';
    }
   }
  }
  cout <<endl;
 }
 getchar();
}

 

 

解决一个数学问题:在方阵内填数,是横行、数列的和相等,请输入您准备建立的方阵行或列的元素个数,请输入奇数(偶数无解)。

 

发觉在后边的输出段时再时间复杂度太高了,有待改善。

评论 (3)

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

福兴韵发表:
我对链表比较陌生 多多指点
4 月 10 日
匿名 的图片
魔法小雨 发表:
是高些
1 月 18 日
匿名 的图片
魔法小雨 发表:
说实话,这个题最好用数组解,效率会低些
1 月 18 日

引用通告

此日志的引用通告 URL 是:
http://lanffulce.spaces.live.com/blog/cns!1A5890B2A01DF83!116.trak
引用此项的网络日志