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. BZOJ 2351 Matrix(哈希)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2351 题意:给出一个n*m的01矩阵.再给出10个A*B的小01矩阵.判断这些小的矩阵是 ...

  2. core--线程同步(用户模式)

    用户模式下的多线程同步只适用用于同一个进程内的多个线程,其范围使用于读写问题:比如一本书,必须是作者A写完之后,读者B才能够读取.否则作者一边修改,读者一边读,完全乱套. 线程读者B如何能在多线程环境 ...

  3. Qt之QHeaderView添加复选框

    简述 前面分享了QTableView中如何添加复选框.本节主要介绍QTableView中的表头-QHeaderView添加复选框的功能,下面以水平表头为例,垂直表头类似! 简述 效果 QHeaderV ...

  4. 通知(NSNotification)

    通知 一个完整的通知一般包含3个属性: - (NSString *)name; // 通知的名称 - (id)object; // 通知发布者(是谁要发布通知) - (NSDictionary *)u ...

  5. NBUT 1120 Reimu's Teleport (线段树)

    题意: 有n个格子,一开始全部面向top.接下来的每次修改F a b ,如果 a>b则将a~b之间的格子全面置为向右,否则置为向左.对于每个询问Q输出向左.top.右的数量. 思路: 普通线段树 ...

  6. ecshop 点购物车弹出提示框

    1.找到common.js在最下面输入以下代码 * 点击购物后弹出提示层 * Chen 2010.7.28 * 参数 cartinfo:购物车信息 */function openDiv_chen(ca ...

  7. android 语言切换过程分析

    android 语言切换过程分析 2014-02-27 18:13 1207人阅读 评论(0) 收藏 举报 语言切换android语言切换android改变语言 最近在看一个bug,系统切换语言后,本 ...

  8. uimodalpresentationformsheet resize ios7

    CROHomeCRAAddController *temp =[[CROHomeCRAAddControlleralloc] init]; temp.modalTransitionStyle = UI ...

  9. 射手网字幕打包下载(73.16G)

    射手网陪着我度过15年了. 我所希望射手网所具有的价值,就是能令更多人跨越国家的樊篱,了解世界上不同的文化. 如果这个网站有帮到人,我就已经很满足了. 但是,需要射手网的时代已经走开了. 因此,今天, ...

  10. 定时组件quartz系列<二>quartz的集群原理

    1.基本信息:      Quartz是一个开源的作业调度框架,它完全由java写成,并设计用于J2Se和J2EE应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它 来为执行一个作业而创建简单的或 ...