zoukankan      html  css  js  c++  java
  • cogs 1811. [NOIP2014]螺旋矩阵

    ★   输入文件:matrixc.in   输出文件:matrixc.out   简单对比

    时间限制:1 s   内存限制:256 MB

    【题目描述】

    MLE:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    
    using namespace std;
    const int N=10010;
    
    long long a[N][N];
    int n,x,y;
    
    int main()
    {
    	freopen("matrixc.in","r",stdin);
    	freopen("matrixc.out","w",stdout);
    	scanf("%d%d%d",&n,&x,&y);
    	long long tot=0;
    	int i=1,j=0;
    	long long nn=n*n;
    	while(1)
    	{
    		while((++j)<=n&&!a[i][j])
    			a[i][j]=(++tot);
      		j--;
    		while((++i)<=n&&!a[i][j])
    			a[i][j]=(++tot);
    		i--;
    		while((--j)>=1&&!a[i][j])
    			a[i][j]=(++tot);
    		j++;
    		while((--i)>=1&&!a[i][j])
    			a[i][j]=(++tot);
    	    i++;
    		if(tot>=nn)
    		{
    			printf("%d",a[x][y]);
    			return 0;
    		}
    	}
    	return 0;
    	
    }
    

    找规律:

    #include<stdio.h>
    int main()
    {	freopen("matrixc.in","r",stdin);
    	freopen("matrixc.out","w",stdout);
        int n,i,j;
        int m;//m表示总共的层数 
        int k,p,q;//循环变量 
        int flag=0;//标志性变量:等于0表示尚未循环到目标元素(i,j) 
        int t;
         int len;
         
        scanf("%d%d%d",&n,&i,&j);
        m=(n+1)/2;  //m表示总共的层数 
        t=1;        //t表示要填进数组的数字
        for(k=1;k<=m&&flag==0;k++)
        {
            p=k,q=k;      //(k,k)是第k层左上角坐标点
            len=n-2*(k-1);//表示当前层中每一条边的元素个数 
            for(;q<=(k+len-1);q++)//填充当前层的顶边 
            {
                if(p==i&&q==j)
                {
                    printf("%d
    ",t);
                    return 0;
                }
                t++;
            }
            q--;
            p++;
            for(;p<=(k+len-1);p++)//填充当前层的右边 
            {
                if(p==i&&q==j)
                {
                    printf("%d
    ",t);
                    return 0;
                }
                t++;
            }
            p--;
            q--;
            for(;q>=k;q--)//填充当前层的下边
            {
                if(p==i&&q==j)
                {
                    printf("%d
    ",t);
                    return 0;
                }
                t++;
            }
            q++;
            p--;
            for(;p>k;p--)//填充当前层的左边
            {
                if(p==i&&q==j)
                {
                    printf("%d
    ",t);
                    return 0;
                }
                t++;
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    ruby and watir学习之_正则表达式
    ruby and watir学习之_String 类
    ruby and watir学习之_Array 类
    ruby and watir学习之_Numeric类&&Float 类
    ruby and watir学习之_Hash 类
    solrj使用demo
    Apache Solr索引富文本(html word pdf)
    TPCC简单计算法
    Apache Solr solrconfig.xml 中文说明
    Apache Solr的索引和查询顺序
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7223674.html
Copyright © 2011-2022 走看看