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. 怎样在Ubuntu系统安装可用的QQ

    http://jingyan.baidu.com/article/9f63fb91d1f6bbc8400f0e1c.html

  2. Order to Cash Process

    order to cash process steps can be listed as below · Enter the Sales Order · Book the Sales Order · ...

  3. js中的json对象详细介绍

    JSON一种简单的数据格式,比xml更轻巧,在JavaScript中处理JSON数据不需要任何特殊的API或工具包,下面为大家详细介绍下js中的json对象, 1.JSON(JavaScript Ob ...

  4. Storm安装与实验

    接上一篇Kafka的安装与实验: http://www.cnblogs.com/charlesblc/p/6046023.html 还有再上一篇Flume的安装与实验: http://www.cnbl ...

  5. minimum-number-of-arrows-to-burst-balloons(还挺好)

    https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/ 与会议室排期问题,很相似. package com. ...

  6. No resource found that matches the given name

    XML里面明显已经定义了ID,可是android:layout_toLeftOf="@id/text_seller"报错,说没有定义,原来这玩意要写在相对位置对象声明的下面,是有顺 ...

  7. php使用第三方登录

    目前只做了微博和qq的,前面的去connect.qq.com,open.weibo.com注册的步骤省略 qq和weibo站点都有可以现在的php版本的api,qq的api相对高大上一些. <s ...

  8. 【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型

    原文网址:http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/ #!/usr/bin/python 是用 ...

  9. CSS hack——不同浏览器的CSS应对法

    1.IE条件注释法: lte表示“小于等于”,“lt”表示“小于”,“gte”表示“大于等于”,“gt”表示“大于”,“!”表示“不等于”. <!--[if IE 6]> <link ...

  10. sqlite3使用简介(内含解决sqlite内存的方法)

    一.使用流程 要使用sqlite,需要从sqlite官网下载到三个文件,分别为sqlite3.lib,sqlite3.dll,sqlite3.h,然后再在自己的工程中配置好头文件和库文件,同时将dll ...