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. Oracle Order Management DropShip Flow for R12

    Oracle Order Management DropShip Flow for R12 Email ThisBlogThis!Share to TwitterShare to FacebookSh ...

  2. C# Access DBHelp

    /* * By :落落 * URL: Www.MyLuoLuo.Com */ using System; using System.Collections.Generic; using System. ...

  3. DELPHI 里面的迭代

    迭代(Iiterator)的作用:遍历一个集合(Collections)的每一个元素(item). delphi 2005之后新加入一种 for .. in .. 遍历语句,支持String,Set, ...

  4. 在maven项目中使用mybatis-generator-maven-plugin生成mybatis代码

    项目整体的目录结构如下: pom.xml如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  5. Codeforces 377 A Maze【DFS】

    题意:给出n*m的矩阵,矩阵由'.'和'#'组成,再给出k,表示需要在'.'处加k堵墙,使得剩下的'.'仍然是连通的 先统计出这个矩阵里面总的点数'.'为sum 因为题目说了一定会有一个解,所以找到一 ...

  6. Jquery源码中的Javascript基础知识(四)— jQuery.fn.init方法

    $() 即调用了jQuery.fn.init方法 jQuery = function( selector, context ) { return new jQuery.fn.init( selecto ...

  7. Maven之 学习资料

    整理maven的学习资料,长期更新. 一.视频 1.孔浩老师的  maven视频教程 二.博客 1.黄勇:     maven那些事儿 使用 OSC Maven 仓库 三.书籍 1.<Maven ...

  8. iOS中第三方框架刷新

    0.先加入主头文件 #import "MJRefresh.h" 1.添加下拉刷新 MJRefreshHeaderView *header = [MJRefreshHeaderVie ...

  9. Heritrix源码分析(四) 各个类说明(转)

    Heritrix的类的确很繁琐,往往继承了一层又一层,最多的继承好像有7层.下面就一个包一个包的说明每个类的作用,由于里面Heritrix组件分明,很多组件没用到的同时该组件的类我也没怎么接触,所以这 ...

  10. dynamic_cast

    作为四个内部类型转换操作符之一的dynamic_cast和传统的C风格的强制类型转换有着巨大的差别.除了dynamic_cast以外的转换,其行为的都是在编译期就得以确定的,转换是否成功,并不依赖被转 ...