<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"><channel><title>MagicLen</title><link>https://magiclen.org/cn/category/algorithm/</link><description>多元化原创文章，内容包罗万象，有编程语言、网站应用、软件介绍、硬件介绍、操作系统、旅行游记、专题采访、益智问题、文学创作，以及作者们的知识分享和作品分享。</description><language>zh-CN</language><lastBuildDate>Sun, 22 Nov 2026 19:03:46 +0800</lastBuildDate><atom:link href="https://magiclen.org/cn/category/algorithm/feed/" rel="self" type="application/rss+xml"/><sy:updatePeriod>hourly</sy:updatePeriod><sy:updateFrequency>8</sy:updateFrequency><sy:updateBase>1970-01-01T00:00+00:00</sy:updateBase><item><title>如何写程序计算时间和日期的差距？</title><link>https://magiclen.org/cn/date-differencer/</link><description><![CDATA[<img src="https://magiclen.org/wp-content/uploads/2023/01/arrow-2085192_1920-220x162.jpg" srcset="https://magiclen.org/wp-content/uploads/2023/01/arrow-2085192_1920-220x162.jpg 1x, https://magiclen.org/wp-content/uploads/2023/01/arrow-2085192_1920-440x324.jpg 2x, https://magiclen.org/wp-content/uploads/2023/01/arrow-2085192_1920-660x486.jpg 3x" alt>
有时候我们会需要写程序去计算两个时间点的差距，以求得年龄、经过了多少的时间，或是否还在某个期限之内等等的信息。日(day)与时、分、秒的差距很好算，因为它们是固定的时间（不考虑闰秒）。但年、月就不一样了，一个月可能有 28 至 31 天，一年可能有 365 或 366 天，我们无法直接将两个时间点（的时间戳记）相减之后去除上一个固定的单位（例如拿相差的秒数去除以 2592000 秒）来算出隔了多少...]]></description><category>研究分享</category><category>Rust</category><category>算法</category><category>JavaScript</category><category>JavaScript</category><category>Rust</category><category>TypeScript</category><category>烂 Code</category><category>date-differencer</category><guid>https://magiclen.org/cn/date-differencer</guid><pubDate>Tue, 24 Jan 2023 12:00:00 +0800</pubDate><dc:creator>Magic Len</dc:creator></item><item><title>如何写程序画出正六边形的蜂嵌套网格？</title><link>https://magiclen.org/cn/regular-hexagon-grid/</link><description><![CDATA[<img src="https://magiclen.org/wp-content/uploads/2022/06/hex-g13f0488b9_1920-220x162.jpg" srcset="https://magiclen.org/wp-content/uploads/2022/06/hex-g13f0488b9_1920-220x162.jpg 1x, https://magiclen.org/wp-content/uploads/2022/06/hex-g13f0488b9_1920-440x324.jpg 2x, https://magiclen.org/wp-content/uploads/2022/06/hex-g13f0488b9_1920-660x486.jpg 3x" alt>
所谓的密铺(tessellation)是用一种或多种平面形状去填满一个更大的平面。用正六边形(regular hexagon)来密铺成网格(grid)，会比用矩形密铺还来得更复杂，造成更艺术的视觉效果。那么要如何写程序来画出这个复杂的正六边形网格呢？]]></description><category>研究分享</category><category>算法</category><category>JavaScript</category><guid>https://magiclen.org/cn/regular-hexagon-grid</guid><pubDate>Tue, 23 Aug 2022 12:00:00 +0800</pubDate><dc:creator>Magic Len</dc:creator></item><item><title>如何写程序画出正多边形？</title><link>https://magiclen.org/cn/regular-polygon/</link><description><![CDATA[<img src="https://magiclen.org/wp-content/uploads/2022/06/pentacontagon-220x162.png" srcset="https://magiclen.org/wp-content/uploads/2022/06/pentacontagon-220x162.png 1x, https://magiclen.org/wp-content/uploads/2022/06/pentacontagon-440x324.png 2x, https://magiclen.org/wp-content/uploads/2022/06/pentacontagon-660x486.png 3x" alt>
N 边形，有 N 个顶点、N 个边。每个边都是由两个顶点连接组成，因此只要找到多边形的各顶点座标，就可以轻易地连接画出多边形了！而所谓的正多边形，它的边都是等长的，且任意相邻两边之间所形成的夹角也都是相等的。]]></description><category>编程</category><category>研究分享</category><category>算法</category><category>JavaScript</category><guid>https://magiclen.org/cn/regular-polygon</guid><pubDate>Thu, 18 Aug 2022 12:00:00 +0800</pubDate><dc:creator>Magic Len</dc:creator></item><item><title>如何判断两个浮点数是否相等？是否能整除？相除后余数是多少？</title><link>https://magiclen.org/cn/float-precision/</link><description><![CDATA[<img src="https://magiclen.org/wp-content/uploads/2022/06/geometry-1044090_1920-220x162.jpg" srcset="https://magiclen.org/wp-content/uploads/2022/06/geometry-1044090_1920-220x162.jpg 1x, https://magiclen.org/wp-content/uploads/2022/06/geometry-1044090_1920-440x324.jpg 2x, https://magiclen.org/wp-content/uploads/2022/06/geometry-1044090_1920-660x486.jpg 3x" alt>
使用电脑去进行浮点数运算的时候，在某些条件下会出现显而易见的误差，而这样的误差，就会导致与浮点数有关的计算或是逻辑判断出现问题。]]></description><category>研究分享</category><category>Java</category><category>Rust</category><category>算法</category><category>JavaScript</category><category>Java</category><category>JavaScript</category><category>Rust</category><category>TypeScript</category><guid>https://magiclen.org/cn/float-precision</guid><pubDate>Thu, 21 Jul 2022 12:00:00 +0800</pubDate><dc:creator>Magic Len</dc:creator></item><item><title>用动态规划解决问题：零壹背包问题(0/1 Knapsack Problem)</title><link>https://magiclen.org/cn/dynamic-programming-0-1-knapsack-problem/</link><description><![CDATA[<img src="https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-220x162.jpg" srcset="https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-220x162.jpg 1x, https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-440x324.jpg 2x, https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-660x486.jpg 3x" alt>
一个背着背包的小偷闯空门偷东西，他必须趁屋主回来之前把有价值的物品塞进包包内带走。考虑到小偷自身的行动力，背包能装的物品总重量有限，小偷要如何选择物品才能获得最高的总价值？]]></description><category>研究分享</category><category>Go</category><category>Java</category><category>Rust</category><category>算法</category><category>JavaScript</category><category>Java</category><category>JavaScript</category><category>Rust</category><category>TypeScript</category><category>动态规划</category><category>Go</category><guid>https://magiclen.org/cn/dynamic-programming-0-1-knapsack-problem</guid><pubDate>Tue, 4 Aug 2020 12:00:41 +0800</pubDate><dc:creator>Magic Len</dc:creator></item><item><title>用动态规划解决问题：找零钱问题(Coin Change Problem)</title><link>https://magiclen.org/cn/dynamic-programming-coin-change/</link><description><![CDATA[<img src="https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-220x162.jpg" srcset="https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-220x162.jpg 1x, https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-440x324.jpg 2x, https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-660x486.jpg 3x" alt>
许多人认为身上如果带太多的零钱会让行动变得不方便，因此会希望商店店员在找零钱的时候能够以最少的硬币数来找，而不是全部都用 1 元塞给我们。]]></description><category>研究分享</category><category>Go</category><category>Java</category><category>Rust</category><category>算法</category><category>JavaScript</category><category>Java</category><category>JavaScript</category><category>Rust</category><category>TypeScript</category><category>动态规划</category><category>Go</category><guid>https://magiclen.org/cn/dynamic-programming-coin-change</guid><pubDate>Tue, 28 Jul 2020 12:00:30 +0800</pubDate><dc:creator>Magic Len</dc:creator></item><item><title>用动态规划解决问题：基本观念（有重叠子问题的问题）</title><link>https://magiclen.org/cn/dynamic-programming-basic/</link><description><![CDATA[<img src="https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-220x162.jpg" srcset="https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-220x162.jpg 1x, https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-440x324.jpg 2x, https://magiclen.org/wp-content/uploads/2020/04/technology-1283624_1920-660x486.jpg 3x" alt>
动态规划（Dynamic Programming，简称 DP）是一种解决问题的技巧，主要被用来优化那些「记不住自己过去曾解出来的答案所以只好重复再解」的算法，让它们可以「记忆」已经找出来的答案，从而不断利用，以大大降低时间复杂度（从指数级降到线性）。]]></description><category>研究分享</category><category>Go</category><category>Java</category><category>Rust</category><category>算法</category><category>JavaScript</category><category>Java</category><category>JavaScript</category><category>Rust</category><category>TypeScript</category><category>费氏数列</category><category>动态规划</category><category>Go</category><guid>https://magiclen.org/cn/dynamic-programming-basic</guid><pubDate>Tue, 21 Jul 2020 12:00:16 +0800</pubDate><dc:creator>Magic Len</dc:creator></item><item><title>如何用程序进行质因数分解和寻找最大公因数与最小公倍数？</title><link>https://magiclen.org/cn/prime-factorization-gcd-lcm/</link><description><![CDATA[<img src="https://magiclen.org/wp-content/uploads/2018/10/pay-819587_1920-220x162.jpg" srcset="https://magiclen.org/wp-content/uploads/2018/10/pay-819587_1920-220x162.jpg 1x, https://magiclen.org/wp-content/uploads/2018/10/pay-819587_1920-440x324.jpg 2x, https://magiclen.org/wp-content/uploads/2018/10/pay-819587_1920-660x486.jpg 3x" alt>
若正整数 a 除以正整数 b 可以整除，则称 b 为 a 的因数(Factor)，a 为 b 的倍数(Multiple)，1 是所有正整数最小的因数，任意正整数最大的因数就是该正整数本身。若 a 同时是 x 和 y 的因数，则称 a 是 x 和 y 的公因数(Common Divisor)，如果 a 是 x 和 y 的公因数中最大的一个，则称 a 是 x 和 y 的最大公因数（Greatest C...]]></description><category>研究分享</category><category>Go</category><category>Java</category><category>数学逻辑</category><category>Rust</category><category>算法</category><category>JavaScript</category><category>Java</category><category>JavaScript</category><category>Rust</category><category>TypeScript</category><category>质因数分解</category><category>最大公因数</category><category>最小公倍数</category><category>辗转相除法</category><category>Go</category><guid>https://magiclen.org/cn/prime-factorization-gcd-lcm</guid><pubDate>Thu, 9 Jul 2020 12:00:35 +0800</pubDate><dc:creator>Magic Len</dc:creator></item><item><title>电脑是怎么进行四则运算的？前序式、中序式、后序式又是什么？</title><link>https://magiclen.org/cn/arithmetic/</link><description><![CDATA[<img src="https://magiclen.org/wp-content/uploads/2020/04/addition-black-and-white-black-and-white-chalk-374918-220x162.jpg" srcset="https://magiclen.org/wp-content/uploads/2020/04/addition-black-and-white-black-and-white-chalk-374918-220x162.jpg 1x, https://magiclen.org/wp-content/uploads/2020/04/addition-black-and-white-black-and-white-chalk-374918-440x324.jpg 2x, https://magiclen.org/wp-content/uploads/2020/04/addition-black-and-white-black-and-white-chalk-374918-660x486.jpg 3x" alt>
我们从小学习算术的时候便知道「四则（加、减、乘、除）运算」的规则，也就是「先乘除、后加减，以及括号先算」。那么如果我们要撰写一个可以支持算式输入且能够按照四则运算规则求出结果的程序，该怎么做呢？]]></description><category>研究分享</category><category>Go</category><category>Java</category><category>Rust</category><category>算法</category><category>JavaScript</category><category>Java</category><category>JavaScript</category><category>Rust</category><category>TypeScript</category><category>四则运算</category><category>后序式</category><category>深度优先搜索</category><category>Go</category><guid>https://magiclen.org/cn/arithmetic</guid><pubDate>Thu, 18 Jun 2020 12:00:21 +0800</pubDate><dc:creator>Magic Len</dc:creator></item><item><title>费氏搜索(Fibonacci Search)算法，运用费氏数列的搜索算法</title><link>https://magiclen.org/cn/fibonacci-search/</link><description><![CDATA[<img src="https://magiclen.org/wp-content/uploads/2016/04/Searchicons-search-blogs-512-220x162.png" srcset="https://magiclen.org/wp-content/uploads/2016/04/Searchicons-search-blogs-512-220x162.png 1x, https://magiclen.org/wp-content/uploads/2016/04/Searchicons-search-blogs-512-440x324.png 2x, https://magiclen.org/wp-content/uploads/2016/04/Searchicons-search-blogs-512-660x486.png 3x" alt>
费氏搜索(Fibonacci Search)算法有点像是二元搜索(Binary Search)算法，同样是在一个已排序好的数组中搜索元素，但是它在移动数组索引值时是去参考费氏数列(Fibonacci Sequence)，而不是像二元搜索法那样总是去取索引的中间值。也由于费氏搜索法在移动数组索引值时只需要进行加减运算，不需乘、除法，因此它适合被用在不擅长处理乘、除法的 CPU 上。]]></description><category>研究分享</category><category>Go</category><category>Java</category><category>Rust</category><category>算法</category><category>JavaScript</category><category>Java</category><category>JavaScript</category><category>Rust</category><category>搜索算法</category><category>二元搜索</category><category>TypeScript</category><category>费氏数列</category><category>费氏搜索</category><category>Go</category><guid>https://magiclen.org/cn/fibonacci-search</guid><pubDate>Thu, 28 May 2020 12:00:15 +0800</pubDate><dc:creator>Magic Len</dc:creator></item></channel></rss>