- 论坛徽章:
- 0
|
///////////////////////////////////////接龙完成///////////////////////////////////////////////
#include"iostream.h"
#include"time.h"
#include"iomanip.h"
static unsigned long int next=1;
int rand0(void);
void srand1(unsigned int seed);
void Source(int seed);
void Source_return(int seed);
void Source_increase(int seed);
int **a;
void main()
{
int num;
cout<<"输入矩阵的维数(n>=1):";
srand1((unsigned int)time(0));
while(cin>>num)
{
Source(num);
Source_increase(num);
Source_return(num);
cout<<"请输入矩阵的维数(n>=1):";
}
}
int rand0(void)
{
next=next*1103515245+12345;
return(unsigned int)(next/65536)%32768;
}
void srand1(unsigned int seed)
{
next=seed;
}
void Source(int seed)///////////////////////////////////////////////////////给矩阵复值
{
// a=(int**)(malloc(sizeof(int)*seed));
a=new int *[seed];
for(int ii=0;ii<seed;ii++)
{
// a[ii]=(int*)(malloc(sizeof(int)*seed));
a[ii]=new int[seed];
}
for(int i=0;i<seed;i++)
{
for(int j=0;j<seed;j++)
{
a[j]=rand0()%100;
}
}
}
void Source_increase(int seed)////////////////////////////////////////////////接龙开始
{
int *b,temp;
b=new int[seed*seed];
for(int i=0;i<seed;i++)
{
for(int j=0;j<seed;j++)
{
b[i*seed+j]=a[j];
}
}
for(int k=0;k<seed*seed-1;k++)
{
for(int m=k;m<seed*seed;m++)
{
if(b[k]>b[m])
{
temp=b[k];
b[k]=b[m];
b[m]=temp;
}
else
{
}
}
}
for(int qq=0;qq<seed;qq++)
{
for(int qqq=0;qqq<seed;qqq++)
{
a[qq][qqq]=0;
}
}
int p=0,q=0;
int result=seed,add=0;
for(int v=0;v<seed*seed;v++)
{
if(a[p][q]==0)
{
a[p][q]=b[v];
}
else
{
}
if(q==add&&p>add&&p<result)
{
p--;
if(p==add)
{
p++;
result--;
add++;
}
}
if(p==(result-1)&&q>add&&q<result)
{
q--;
}
if(q==(result-1)&&p>add-1&&p<result-1)
{
p++;
}
if(p==add&&q>add-2&&q<result-1)
{
q++;
}
}
cout<<endl<<endl;
}
void Source_return(int seed)///////////////////////////////////////////////////打印矩阵
{
cout<<setfill(' ');
for(int x=0;x<seed;x++)
{
for(int y=0;y<seed;y++)
{
cout<<setw(5)<<a[x][y];
if(y==seed-1)
{
cout<<endl<<endl;
}
}
}
cout<<endl<<endl;
} |
|