算法-螺旋队列

问题

21  22  ...
20  7   8   9   10
19  6   1   2   11
18  5   4   3   12
17  16  15  14  13

看清以上数字排列的规律,设 1 点的坐标是 (0,0),x 方向向右为正,y 方向向下为正。例如,7 的坐标为 (-1,-1),2 的坐标为 (0,1),3 的坐标为 (1,1)。编程实现输入任意一点坐标 (x,y),输出所对应的数字。

分析

以(0,0) 即 数字 1 为中心,螺旋的形式如图

圆心为第0圈,往外为第1圈,再往外为第2圈,依次类推

每一圈数字的右上角的点p, 它的数值是可以推导出来的

n = 0  -> p =  1   即 (2 * 0 + 1) ^ 2

n = 1  -> p = 9    即 (2 * 1 + 1) ^ 2

n = 2  -> p = 25  即 (2 * 2 + 1) ^ 2

所以,第n圈,右上角的数值为 p = (2n+1)^2,故而,

p1 = p - n

p2 = p - 3n

p3 = p - 5n

p4 = p - 7n

知道了以上点的数值之后,就可以计算任意一点(x,y)处的数值了。

代码

GithubRelated: deficit reverse lunge muscles worked, flight attendant jobs columbus, ohio, canva transparent background video, apartment for rent in grenada long term, ernesto fonseca carrillo net worth, worst commercials 2020, monroe clark middle school shooting, burton riglet park locations, how many bananas does dole sell a year, carrollton city council, harmonic drive disadvantages, precast concrete pool texas, new smyrna beach director of development services, signs your bpd girlfriend is cheating, velvet ana and alberto baby,Related: dwarf redbud varieties, reasons why nigeria should not be divided, pampered chef quick cooker chicken and rice, david brenner ucsd wife, division of gellibrand, thomas o'connor massapequa obituary, xiaomi competitor analysis, omak school district salary schedule, john carney director brother jim, prospect heights shooting, how to plant st augustine grass plugs, maryland police funeral today, jeffrey rogers arlington, tx, how to cleanse evil eye bracelet, alamogordo funeral home obituaries,Related: super star car wash family plan, secrets playa mujeres room service menu, mercer house crime scene photos, are tee higgins and rashard higgins related, how far is bulgaria from ukraine, diffuser shuts off after a few seconds, virgo man obsessed with scorpio woman, kansas city serial killer barrels, is virginia executive order 51 still in effect, newbury park high school yearbook, shirley graham obituary, cva northwest conversion kit, covid recovery certificate for travel to usa, mark frissora wife, legal graffiti walls in ohio,Related: can i eat seafood after helix piercing, 4 point scale rubric advantages and disadvantages, psychosocial rehabilitation goals and objectives examples, video of latasha harlins shooting, allegany county, ny property tax auction 2021, pistachio shot with disaronno, truist bank back check endorsement, what was a franc worth in 1880, what happens if a baby eats a cigarette, magician and tower combination, craftsman 2800 psi pressure washer troubleshooting, python turtle move with arrow keys, ley lines in georgia, ronnie dunn plane crash, dr curry psychologist muffins,Related: paul cornfed'' schneider sammy the bull, women's heptathlon javelin throw, beaver primary school oldham, grand canyon university basketball coach salary, music row happy hour, norwood hills flooring, alexandra bruckheimer net worth, who is the guest on fresh air today, boxing ring dimensions, jonathan edwards daughter temper, vanderbilt nurse medication error cms report, read file from public folder react, 0603 resistor footprint, nyatoh vs mahogany guitar, contribution of quantitative research in social science,Related: meadows funeral home in albany, georgia, what element can beat lightning in prodigy, beth mead danielle van de donk timeline, rotating restaurant nashville, eckington bridge swimming, lakeview elementary school yearbook, day of atonement leviticus 16:11 19, lake county, florida power outage, enoch powell grandchildren, jonina dourif obituary, ricky gervais house marlow, the citizen hotel sacramento haunted, stone county, mississippi mugshots, aqueduct racetrack covid vaccine appointments, is duncan weir related to doddie weir,Related: lord willing and the creek don't rise racist, birth certificate document number i9, rick goodman son of vestal goodman, prescriptive feedback, end to end predictive model using python, custom family birthstone bracelets, us airmail 8 cent stamp value, map of highway 19 in florida, hoarders shanna update, expensive things that start with the letter t, chris romano related to ray romano, burton gilliam car dealership, sentences that sound like they make sense but don't, scotiabank senior client relationship manager salary, ryan kavanaugh brother,

作者:JarvisChu
原文链接:算法-螺旋队列
版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0

发表评论