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. bzoj2748 [HAOI2012]音量调节 背包

    Description 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都要改变一次音量.在演出开始之前,他已经做好了一个列表,里面写着在每首歌开始之前他想要改 ...

  2. Linux(9):期中架构(1)--- 集群构架 & 备份服务

    01. 了解集群架构服务器组成 基本架构组成:(用于让用户进行访问) # 前端服务部分: 1)顾客-用户 是一个访问者,请求访问网站页面 2)保安-防火墙设备 对访问架构用户进行策略控制,正常访问网站 ...

  3. 降雨量 BZOJ 1067

    降雨量 [问题描述] 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,20 ...

  4. 并发安全问题之HashMap

    原文地址: http://my.oschina.net/xianggao/blog/393990#OSC_h2_1 目录[-] 并发问题的症状 多线程put后可能导致get死循环 多线程put的时候可 ...

  5. SQL Server 命令行操作

    连接sqlcmd -S localhost -U SA -P '123456'; 执行脚本 sqlcmd -S localhost -U SA -P '123456' -i /root/dbo.sql ...

  6. java使用jxl,自动导出数据excle,quartz自动发送邮件

    =============JAVA后台代码===================== package com.qgc.service.autoSendMsg.AutoSendMsg import ja ...

  7. OSI模型详解

    OSI 七层模型通过七个层次化的结构模型使不同的系统不同的网络之间实现可靠的通讯,因此其最主要的功能就是帮助不同类型的主机实现数据传输 . 完成中继功能的节点通常称为中继系统.在OSI七层模型中,处于 ...

  8. 洛谷——P3576 [POI2014]MRO-Ant colony

    P3576 [POI2014]MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. ...

  9. Volley 源码解析 StringRequest解析

    Android Vollety是一个很有用的框架,所以想借鉴前人思想,分析这个源代码. 参考: http://blog.csdn.net/crazy__chen/article/details/464 ...

  10. 《深入理解mybatis原理》 Mybatis初始化机制详解

    对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章将通过以下几点详细介绍MyBatis的初始化过程. 1.MyBatis的初始化做了什么 2. MyBatis基于XML配置 ...