10893 Spiral

时间限制:1000MS  内存限制:65535K

题型: 编程题   语言: 无限制

Description

Given an odd number n, we can arrange integers from 1 to n*n in the shape of a spiral. The figure 2.4.1 below illustrates the spiral made by integers from 1 to 25.

【图片】

21  22  23  24  25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13

As we see above, each position in the spiral corresponds to a unique integer. For example, the number in row 1, column 1 is 21, and integer 16 is in row 5, column 2.

Now, given the odd number n (1≤n≤32768), and an integer m (1≤m≤n*n), you should write a program to find out the position of m.

输入格式

The first line of the input is a positive integer T(T≤20). T is the number of the test cases followed. Each case consists of two integer n and m as described above.

输出格式

For each case, output the row number and column number that the given integer is in, separated by a single whitespace. Please note that the row and column number are both starting from 1.

输入样例

3
3 9
5 21
5 16

输出样例

1 3
1 1
5 2

来源

zsu

作者

200831000423

解题思路

上年校赛选拔的时候没做出来却将蛇形矩阵的规律找出来并打印出来,后来无疑肯定是TLE。数据量大就得找规律,解题的办法是找到这个数m在哪个圈子里。通过5*5矩阵可以找出斜线上在不同圈子里数之间的关系,打表存储,在给出数据的时候通过打表的值判断这个数在哪个圈子里然后在这个圈子里找对应的数的位置,具体看实现的代码

 #include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define MAXN 34000 using namespace std; int list[MAXN/]; void init()
{//打表存储斜线上的值
for(int i=; i<MAXN/; ++i)
{
if(!i) list[i] = ;
else list[i] = *i-+list[i-];
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("F:\\test\\input.txt", "r", stdin);
#endif // ONLINE_JUDGE
init();
int T, row, column;
cin>>T;
while(T--)
{
int n, m;
cin>>n>>m;
int circle = -, cnt = , dis;
while(list[++circle] < m);
column = row = n/+-circle;
dis = list[circle] - m; //仍需要移动的步数
//找到圈子的情况下进一步处理数据找到最终的位置
if(dis <= circle*)
{
if(dis <= circle*) row = row + dis;
else
{
row = row + circle*;
column = column + (dis - circle*);
}
}
else
{
int temp = dis - circle*;
if(temp <= circle*-)
{
row = row + circle* - temp;
column = column + circle*;
}
else
{
temp = temp - (circle*-);
row = row + ;
column = column + circle* - temp;
}
}
cout<<row<<" "<<column<<endl;
} return ;
}

SCAU 10893 Spiral的更多相关文章

  1. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  2. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  3. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  4. 【leetcode】Spiral Matrix

    题目概要: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spi ...

  5. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  6. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  7. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  8. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  9. Leetcode Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

随机推荐

  1. hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...

  2. JAVA 方法或者类的注释快捷键

    JAVA 方法或者类的注释快捷键 /*** 登录验证* @param 传入的* @return* @throws Exception*/这种注释效果 方法: 1.先敲“/”在敲两个**,然后回车 方法 ...

  3. Hbase源码分析:Hbase UI中Requests Per Second的具体含义

    Hbase源码分析:Hbase UI中Requests Per Second的具体含义 让运维加监控,被问到Requests Per Second(见下图)的具体含义是什么?我一时竟回答不上来,虽然大 ...

  4. MINA经典入门例子----Time Server

    原文地址 http://blog.sina.com.cn/s/blog_720bdf0501010b8r.html 貌似java的IO.NIO的入门例子都有相关的Time Server Demo.本例 ...

  5. UVa 297 (四分树 递归) Quadtrees

    题意: 有一个32×32像素的黑白图片,用四分树来表示.树的四个节点从左到右分别对应右上.左上.左下.右下的四个小正方区域.然后用递归的形式给出一个字符串代表一个图像,f(full)代表该节点是黑色的 ...

  6. [Warning] TIMESTAMP with implicit DEFAULT value is deprecated

    As indicated by the warning, to turn off the nonstandard behaviors, enable the new explicit_defaults ...

  7. WWDC 2015 - 概记

    WWDC 2015已经过去快一个月了,今年似乎没有像去年那样变化巨大,一切都在慢慢演进,iOS.Mac OS.watchOS都变得越来越好. 新的三大平台的发布,iOS 9/Mac OS EL Cap ...

  8. php延迟加载的示例

    class a{ ; ; //public $d = 5; public function aa(){ echo self::$b; } public function cc(){ echo stat ...

  9. 【英语】Bingo口语笔记(48) - 关于春节的表达

  10. 【英语】Bingo口语笔记(73) - 以tly,tely结尾的误读