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();
}
解决一个数学问题:在方阵内填数,是横行、数列的和相等,请输入您准备建立的方阵行或列的元素个数,请输入奇数(偶数无解)。
发觉在后边的输出段时再时间复杂度太高了,有待改善。