SCAU 10893 Spiral
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矩阵可以找出斜线上在不同圈子里数之间的关系,打表存储,在给出数据的时候通过打表的值判断这个数在哪个圈子里然后在这个圈子里找对应的数的位置,具体看实现的代码
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #define MAXN 34000 7 8 using namespace std; 9 10 int list[MAXN/2]; 11 12 void init() 13 {//打表存储斜线上的值 14 for(int i=0; i<MAXN/2; ++i) 15 { 16 if(!i) list[i] = 1; 17 else list[i] = 8*i-2+list[i-1]; 18 } 19 } 20 21 int main() 22 { 23 #ifndef ONLINE_JUDGE 24 freopen("F:\\test\\input.txt", "r", stdin); 25 #endif // ONLINE_JUDGE 26 init(); 27 int T, row, column; 28 cin>>T; 29 while(T--) 30 { 31 int n, m; 32 cin>>n>>m; 33 int circle = -1, cnt = 0, dis; 34 while(list[++circle] < m); 35 column = row = n/2+1-circle; 36 dis = list[circle] - m; //仍需要移动的步数 37 //找到圈子的情况下进一步处理数据找到最终的位置 38 if(dis <= circle*4) 39 { 40 if(dis <= circle*2) row = row + dis; 41 else 42 { 43 row = row + circle*2; 44 column = column + (dis - circle*2); 45 } 46 } 47 else 48 { 49 int temp = dis - circle*4; 50 if(temp <= circle*2-1) 51 { 52 row = row + circle*2 - temp; 53 column = column + circle*2; 54 } 55 else 56 { 57 temp = temp - (circle*2-1); 58 row = row + 1; 59 column = column + circle*2 - temp; 60 } 61 } 62 cout<<row<<" "<<column<<endl; 63 } 64 65 return 0; 66 }

更多内容请关注个人微信公众号 物役记 (微信号:materialchains)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?