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. 【bzoj2733】[HNOI2012]永无乡 线段树合并

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

  2. GFS, HDFS, Blob File System架构对比

    分布式文件系统很多,包括GFS,HDFS,淘宝开源的TFS,Tencent用于相册存储的TFS (Tencent FS,为了便于区别,后续称为QFS),以及Facebook Haystack.其中,T ...

  3. COdevs 1074 食物链

    1074 食物链 2001年NOI全国竞赛  时间限制: 3 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题目描述 Description 动物王国中有三类动物 A,B,C, ...

  4. 使用Sharesdk实现第三方平台登录(qq,新浪微博)

    首先到sharesdk开放píng台下载demo ,以下要用到的文件来自于 simple里面 第一步:导入官方的jar包    第二步:添加ShareSDK.xml文件并修改相关píng台key  第 ...

  5. hdu 4885 (n^2*log(n)判断三点共线建图)+最短路

    题意:车从起点出发,每次只能行驶L长度,必需加油到满,每次只能去加油站或目的地方向,路过加油站就必需进去加油,问最小要路过几次加油站. 开始时候直接建图,在范围内就有边1.跑最短了,再读题后发现,若几 ...

  6. Codeforces 665C Simple Strings【暴力,贪心】

    题目链接: http://codeforces.com/contest/665/problem/C 题意: 改变最少的字符,使得最终序列无相同的连续的字符. 分析: 对每一个与前一个字符相同的字符,枚 ...

  7. docker 配置 direct-lvm

    当前需要设置的宿主机是环境是搭建在vbox虚拟机上的centos7系统.测试环境中出现过一次意外情况,当时为了测试docker日志文件限制,运行了一个docker容器,但是后面忘记停止了,几天后发现了 ...

  8. flask的debug模式下,网页输入pin码进行调试

    网站后端Python+Flask .FLASK调试模式之开启DEBUG与PIN使用? 自动加载: # 方式一 1 2 if __name__ == '__main__':     app.run(ho ...

  9. Linux Shell高级技巧

    Linux Shell高级技巧(一) http://www.cnblogs.com/stephen-liu74/archive/2011/12/22/2271167.html一.将输入信息转换为大写字 ...

  10. 【Android小项目】找不同,改编自&quot;寻找房祖名&quot;的一款开源小应用。

    近期在微信朋友圈"寻找房祖名"和"万里寻刀"这类小游戏比較火.我试着写了一个android版本号的,里面全是一系列的形近字,实现原理非常easy:用一个Grid ...