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;
    }
    

      

  • 相关阅读:
    接口,抽象类,普通类
    将svn项目导出,再导入到其他工作空间
    Ajax 与 Struts 1
    save tran tranName
    hibernate缓存机制详细分析
    sql中的group by 和 having 用法解析
    TensorBoard 实践 1
    Tensorflow 解决MNIST问题的重构程序
    在MNIST数据集,实现多个功能的tensorflow程序
    Tensorflow中的滑动平均模型
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7223674.html
Copyright © 2011-2022 走看看