Contest02-4 Spiral

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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

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 题目分析:输入t组,每组n代表是矩阵的行(==列), 再输入一个数m,输出该数在该矩阵的坐标(x, y)。
算法分析:
找规律 1.先计算出要找的那个数字在从中心往外的螺旋矩阵的第几层上
2.在该圈上的第几个位置,(先假设当前所在的圈是最外圈 )通过下标的变换移动过去
3.如果当前圈是最外圈就直接输出结果, 否则还要计算一下,此圈的外面还有几圈套着,x和y都要再加上套着的圈数输出 代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h> using namespace std; int main()
{
int t;
int i, j;
int n; long long m;
int dd, ff;
int a, b;
int x, y; scanf("%d", &t);
while(t--)
{
scanf("%d %lld", &n, &m);
if( m==1 )
{ printf("%d %d\n",(n+1)/2,(n+1)/2 ); //如果m=1,输出矩阵中心的位置坐标
continue;
} for(i=1; i<=n; i++)
{
if( (i*2-1)*(i*2-1)>=m ) //这是找规律计算的结果,即是中心子矩阵的元素数和
{
break; //计算找到我们要找的那个数在第几圈上
}
}
dd=i; //dd保存我们要找的那个数在第几圈
ff=i-1;
a=(ff*2-1)*(ff*2-1); //计算内部矩阵的数字个数和
int len=m-a; // 计算从特定坐标开始移动的步数到达要到达的数字 b=(dd-1)*2; //一个板长,移动一个b的长度,坐标就要有一个元素转换方向
int cnt=0;
int cc;
while(cnt<len)
{
cc=0;
x=2; y=dd*2-1;
while(cnt<len && cc<b)
{
cnt++;
x++; //往下移动 y不变
cc++;
}
cc=0;
while(cnt<len && cc<b)
{
cnt++;
y--; //往左移动 x不变
cc++;
}
cc=0;
while(cnt<len && cc<b)
{
cnt++;
x--; //在向上移动 y不变
cc++;
}
cc=0;
while(cnt<len && cc<b)
{
cnt++;
y++; //在向右移动 x不变
cc++;
}
}
if(dd==((n+1)/2) )
printf("%d %d\n", x-1, y ); //此处计算的x有一步之差,没有处理bug,因为让x-1就等于正确的坐标
else
{
int kk;
kk=(n+1)/2;
kk=kk-dd;
printf("%d %d\n", x-1+kk, y+kk ); //如果该圈数不是最外圈,坐标加上外面的圈数。
}
}
return 0;
}

sdut oj 1510 Contest02-4 Spiral的更多相关文章

  1. SDUT OJ 2607

    /*http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607*/ 题目大意:给出一个字符串,求出里 ...

  2. SDUT OJ 1221 亲和数 (找出某个数n所有的因子数,只需要暴力:2->sqrt(n) 即可 )

    亲和数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 如果a的因子和等于b,b的因子和等于a,且a≠b,则称a,b为亲和数对. ...

  3. SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )

    图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天 ...

  4. 九度OJ 1510 替换空格

    题目地址:http://ac.jobdu.com/problem.php?pid=1510 题目描述: 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We ...

  5. 【离散数学】 SDUT OJ 传递闭包 && memset 使用注意事项

    传递闭包 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 已知有n头牛,m次战斗关系, ...

  6. SDUT OJ 2783 小P寻宝记

    #include<iostream> #include<memory.h> #define N 10020 using namespace std; int dp[N],pi[ ...

  7. SDUT oj 3005 打怪升级(内存搜索)

    当比赛一直纠缠骑2如何做一个非常大的数量,数组不开啊...后来他们发现自己很傻啊,该数不超过最大10什么,这个上限就是力量100什么.. .. 其它的就是记忆化搜索啊,还有就是加一点力量的瓶子当时就要 ...

  8. SDUT OJ 2463 学校password你必须学会科学计划

    #include<iostream> #include<string.h> #include<stdio.h> #define N 10010 #define M ...

  9. SDUT oj 2610

    /*题目大意:输入一序列n个数字,然后输入m个询问,每个询问包含左边区间和右边区间,还有a和b,问你这个区间内有几个数大于等于a且小于等于b 做法:树状数组,先求出这个区间内有几个数小于a,然后求这个 ...

随机推荐

  1. CCPC-Wannafly Winter Camp Day1 (Div2, online mirror) A,B,C,E,F,I,J

    https://www.zhixincode.com/contest/7/problems A题 分类讨论 当B有点需要经过时 穿梭的花费肯定为2*k,也可以发现,我们要找到包含所有需要经过的点(不含 ...

  2. python 获取时间 存入文件

    1读文件: file_path_name = '/home/robot/bzrobot_ws/src/bzrobot/bzrobot_comm/led_show_data/'+file_name+'. ...

  3. idea tomcat 配置

    昨天我们讲了如何新建多模块项目:idea创建maven多模块项目 本节课,我们讲如何配置tomcat,使昨天配置的web项目,JRapid.Admin可以运行起来.具体步骤如下 第一步 第二步 第三步 ...

  4. SSH login without password

    SSH login without password Your aim You want to use Linux and OpenSSH to automize your tasks. Theref ...

  5. [转]java类 对象 和构造方法

    github地址:https://github.com/lily1010/java_learn/tree/master/dog java中对象和类 java中万物皆对象,比如说动物,里面有猫,狗,鱼等 ...

  6. HDOJ 4003 Find Metal Mineral

    题意: 一棵有权树,从根结点中放入 K 个机器人.求用这 K 个机器人遍历全部的结点最少的权值和. 思路: 1. dp[u][i] 表示给以 u 为根节点的子树放 i 个机器人,遍历其子树所须要的最小 ...

  7. SolidEdge 装配体中如何快速的搞定一个面上所有螺丝 如何在装配体上进行阵列

    1 点击"规则排列" 选择要排列的螺丝   2 选择被规则排列的架子   3 选择所有的圆孔(鼠标滑到任意圆孔位置,左键单击即可选中所有圆孔)   4 选择参考的基准孔(已经上了螺 ...

  8. 华夏互联总经理汪照发接受程序猿杂志专訪-2014年6月江西IDC排行榜

     作为软件开发界最权威的期刊,<程序猿>杂志一直是业界推崇的高端读物,能被其採訪是极大的荣耀. 上个月,作为江西省内的排名第一的站点开发企业和专业的站点研发厂商,Zoomla!逐浪CM ...

  9. ngnix

    nginx的平滑重启 博客分类: nginx nginx平滑重启  在研发过程中,修改nginx的配置文件nginx.conf是很平常的事,需要重启nginx.如果我们直接reload是有一定风险的, ...

  10. Effective C++ 43,44

    43.明智地使用多继承. 多继承带来了极大的复杂性.最主要的一条就是二义性. 当派生类为多继承时,其多个基类有同名的成员时,就会出现二义性.通常要明白其使用哪个成员的.显式地限制修饰成员不仅非常笨拙, ...