免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1580 | 回复: 3
打印 上一主题 下一主题

随机数字接龙矩阵 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-19 12:09 |只看该作者 |倒序浏览
///////////////////////////////////////接龙完成///////////////////////////////////////////////
#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;
}

论坛徽章:
0
2 [报告]
发表于 2006-04-19 12:20 |只看该作者
像是上學時的習作

论坛徽章:
0
3 [报告]
发表于 2006-04-19 12:28 |只看该作者
支持原创  

论坛徽章:
0
4 [报告]
发表于 2006-04-19 14:17 |只看该作者
原帖由 dbcat 于 2006-4-19 12:28 发表
支持原创  

谢谢支持..谢谢支持
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP