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. 《c程序设计语言》读书笔记-字符型0-9转为数字0-9

    #include <stdio.h> #define Num 10 int atoi(char s[]); int main() { int c,i = 0; char s[Num]; i ...

  2. java 反编译插件 JD-Eclipse 和 JD-IntelliJ

    去官网 : http://jd.benow.ca/ 找到 JD-Eclipse 和  JD-IntelliJ,下载,前者安在eclipse上,后者安在as上.

  3. from 表单提交

    html端代码 <body> <div id="dlg" class="easyui-panel" buttons="#dlg-bu ...

  4. hdu4422The Little Girl who Picks Mushrooms

    4422 小于等于3 的时候就是1024 4的时候 讨论 5的时候讨论 注意重量为0的情况 #include <iostream> #include<cstdio> #incl ...

  5. Android 实现布局动态加载

    Android 动态加载布局 通过使用LayoutInflater 每次点击按钮时候去读取布局文件,然后找到布局文件里面的各个VIEW 操作完VIEW 后加载进我们setContentView 方面里 ...

  6. UVa 1442 (线性扫描) Cave

    对于一个水坑,水平面肯定是相等的.(废话,不然为什么叫水ping面) 因为水面不能碰到天花板,所以将水面向两边延伸要么碰到墙壁要么延伸到洞穴外面去. 设h(i)表示向左延伸不会碰到天花板的最高水平面, ...

  7. UVa 129 Krypton Factor【回溯】

    学习的紫书的回溯,理解起来还是好困难的说啊= = #include<iostream> #include<cstdio> #include<cstring> #in ...

  8. 15.Object-C--浅谈Foundation框架OC数组NSArray与NSMutableArray

    昨天总结了一下NSString与NSMutableString,今天我在这里总结一下NSArray与NSMutableArray. NSArray数组是:不可变数组. nil 是数组元素结束的标记.O ...

  9. 介绍 Android 的 Camera 框架

    总体介绍 Android Camera 框架从整体上看是一个 client/service 的架构,有两个进程:一个是 client 进 程,可以看成是 AP 端,主要包括 JAVA 代码与一些 na ...

  10. Android命令行播放MP3音乐

    /*************************************************************************** * Android命令行播放MP3音乐 * 说 ...