-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.xml
105 lines (50 loc) · 85 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>从正则表达式匹配 IP 地址谈起</title>
<link href="2023/10/10/cong-zheng-ze-biao-da-shi-pi-pei-ip-di-zhi-tan-qi/"/>
<url>2023/10/10/cong-zheng-ze-biao-da-shi-pi-pei-ip-di-zhi-tan-qi/</url>
<content type="html"><![CDATA[<h1 id="从正则表达式匹配-ip-地址谈起"><a class="markdownIt-Anchor" href="#从正则表达式匹配-ip-地址谈起"></a> 从正则表达式匹配 IP 地址谈起</h1><p>正则表达式 RegEx 是一种强大的字符串处理工具,在字符串处理中应用广泛。RegEx(Regular Expression)可以理解为匹配规则(Regular)的搜索模式。正则表达式匹配的不是字符串本身,而是一种字符串生成规则。凡是符合这一规则的字符串,都可以使用正则表达式检索获取。</p><p>介绍完正则表达式的概念,这里可以通过一些实践来体会正则表达式的强大与便捷。下面介绍如何使用正则表达式匹配 IP 地址,这里作为练习只处理 IPv4 地址。</p><p>待匹配字符串如下:</p><pre class="line-numbers language-text" data-language="text"><code class="language-text">$ arp -a接口: 192.168.216.1 --- 0x3 Internet 地址 物理地址 类型 192.168.216.255 ff-ff-ff-ff-ff-ff 静态 224.0.0.22 01-00-5e-00-00-16 静态 224.0.0.251 01-00-5e-00-00-fb 静态 224.0.0.252 01-00-5e-00-00-fc 静态 239.255.255.250 01-00-5e-7f-ff-fa 静态接口: 169.254.86.111 --- 0xd Internet 地址 物理地址 类型 169.254.255.255 ff-ff-ff-ff-ff-ff 静态 224.0.0.22 01-00-5e-00-00-16 静态 224.0.0.251 01-00-5e-00-00-fb 静态 224.0.0.252 01-00-5e-00-00-fc 静态 239.255.255.250 01-00-5e-7f-ff-fa 静态接口: 192.168.1.2 --- 0xe Internet 地址 物理地址 类型 192.168.1.1 18-13-2d-2f-fa-b4 动态 192.168.1.255 ff-ff-ff-ff-ff-ff 静态 224.0.0.22 01-00-5e-00-00-16 静态 224.0.0.251 01-00-5e-00-00-fb 静态 224.0.0.252 01-00-5e-00-00-fc 静态 239.255.255.250 01-00-5e-7f-ff-fa 静态 255.255.255.255 ff-ff-ff-ff-ff-ff 静态接口: 169.254.252.27 --- 0x14 Internet 地址 物理地址 类型 169.254.255.255 ff-ff-ff-ff-ff-ff 静态 224.0.0.22 01-00-5e-00-00-16 静态 224.0.0.251 01-00-5e-00-00-fb 静态 224.0.0.252 01-00-5e-00-00-fc 静态 239.255.255.250 01-00-5e-7f-ff-fa 静态 255.255.255.255 ff-ff-ff-ff-ff-ff 静态<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="初级"><a class="markdownIt-Anchor" href="#初级"></a> 初级</h2><p>根据 IPv4 地址的格式,可以发现,IP 地址有 4 组数字组成,每组数字长度范围为从 1 位到 3 位,并且每组数字之间由 <code>.</code> 分割。那么根据这一规律,可以使用正则表达式写出匹配字符串:</p><pre class="line-numbers language-regex" data-language="regex"><code class="language-regex"><span class="token group punctuation">(</span><span class="token group punctuation">(</span><span class="token charclass class-name">\d</span><span class="token quantifier number">{1,3}</span><span class="token group punctuation">)</span><span class="token special-escape escape">\.</span><span class="token group punctuation">)</span><span class="token quantifier number">{3}</span><span class="token group punctuation">(</span><span class="token charclass class-name">\d</span><span class="token quantifier number">{1,3}</span><span class="token group punctuation">)</span><span class="token group punctuation">(</span><span class="token charclass class-name">\d</span><span class="token quantifier number">{1,3}</span><span class="token special-escape escape">\.</span><span class="token group punctuation">)</span><span class="token quantifier number">{3}</span><span class="token group punctuation">(</span><span class="token charclass class-name">\d</span><span class="token quantifier number">{1,3}</span><span class="token group punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><blockquote><p>IP 地址由 4 个字节组成,每个字节 8 位,所以 IPv4 地址的取值范围是 0 ~ 255,共 256 个。</p></blockquote><p>这样的处理方法虽然简单,一目了然,但却需要面对一个大问题——正则表达式匹配出来的不是 IPv4 地址。例如:</p><pre class="line-numbers language-text" data-language="text"><code class="language-text">999.888.777.666......<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>这些非法 IPv4 地址字段也会在这一不严谨的正则表达式中匹配出来,这显然不是我们想要的。因此,我们需要对正则表达式进行改进,使其只匹配合法的 IPv4 地址。</p><h2 id="高级"><a class="markdownIt-Anchor" href="#高级"></a> 高级</h2><p>在构造一个正则表达式的时候,一定要考虑周全,避免出现匹配到不合法的字段,定义清楚的规则,确定可以匹配什么,以及不能匹配什么。</p><p>在这个 IPv4 地址匹配的过程中,我们需要确定一个合法有效的 IPv4 地址的格式,即:</p><ol><li>4 组数字,每组数字范围为 1 ~ 255;</li><li>每组数字之间用 <code>.</code> 分割;</li><li>任意的 1 位或 2 位数字</li><li>任意的以 <code>1</code> 开头的 3 位数字;</li><li>任意的以 <code>2</code> 开头,第二位数字在 <code>0</code> 到 <code>4</code> 之间的 3 位数字;</li><li>任意的以 <code>2</code> 开头,第二位数字为 <code>5</code>,第三位数字在 <code>0</code> 到 <code>5</code> 之间的 3 位数字;</li></ol><p>知晓上述规则后,依次罗列所有规则,准确的匹配模式自然可以构造如下:</p><pre class="line-numbers language-text" data-language="text"><code class="language-text">(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>其中,按照 IPv4 地址规则,</p><ul><li><code>(\d{1,2})</code> 匹配任意 1 位或 2 位数字;</li><li><code>(1\d{2})</code> 匹配任意以 <code>1</code> 开头的 3 位数字;</li><li><code>(2[0-4]\d)</code> 匹配任意以 <code>2</code> 开头,第二位数字在 <code>0</code> 到 <code>4</code> 之间的 3 位数字;</li><li><code>(25[0-5])</code> 匹配任意以 <code>25</code> 开头的 3 位数字;</li></ul><p>看起来不错,完全按照上述 IPv4 规则匹配,但只要实际匹配查找,会发现上述正则表达式,根本无法正常匹配 IPv4 地址,效果甚至不如第一个匹配模式。例如:</p><pre class="line-numbers language-text" data-language="text"><code class="language-text">255.255.255.0255.255.255.10255.255.255.255<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>这两种 Ipv4 地址,上述正则表达式只能匹配到第一个和第二个,却无法匹配到第三个。但是,正则表达式规则是严格按照 IPv4 地址规则定义的,为什么会出现这种情况呢?</p><p>这就要引出正则表达式的贪心(greedy)特性了。也就是在正则表达式中,如果出现多个匹配模式,那么会优先匹配<strong>最长</strong>的匹配模式,而不是符合匹配规则的<strong>最短</strong>的,而且会尽量满足第一个匹配模式,具体到 IPv4 地址匹配,就是尽可能匹配最长的规则以及首位规则。</p><pre class="line-numbers language-text" data-language="text"><code class="language-text">((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这是匹配一组 IPv4 地址字段的正则表达式匹配模式,其中 <code>(\d{1,2})</code> 表示匹配任意 1 ~ 2 位数字,在 IPv4 前三组字段的匹配中,由于 <code>.</code> 的存在,根据正则表达式的贪心特性,会尽可能匹配最长的匹配模式,即 <code>(\d{1,2})</code> 无法匹配的会自动切换下一子表达式,以求匹配的字符串模式更长。所以,上述正则表达式匹配前三组没有出现任何问题,但是在第四组字段的匹配过程中,由于 <code>(\d{1,2})</code> 无法匹配 <code>255</code>,所以当 <code>(\d{1,2})</code> 匹配 <code>25</code> 时,两个数字全部匹配到,匹配模式自动结束,如果第四个字段是 3 个数字,那么第三个数字将被忽略而无法匹配到。</p><p>那么,如何改进呢?</p><h2 id="进阶"><a class="markdownIt-Anchor" href="#进阶"></a> 进阶</h2><p>其实,根据以上分析,可以发现,无需较大修改,因为 IPv4 规则仍是不变的,如果将 IPv4 地址的匹配规则改为如下,就可以完美匹配了。</p><pre class="line-numbers language-text" data-language="text"><code class="language-text">(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))\.){3}(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2})))<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>按照正则表达式的贪心特性以及首位子表达式优先匹配特性,可以在规则组合时,优先选择匹配位数较长的子表达式,当前子表达式不满足时,在依次切换匹配位数较短的子表达式。</p><h2 id="写在最后"><a class="markdownIt-Anchor" href="#写在最后"></a> 写在最后</h2><p>本来只是正则表达式匹配 IPv4 地址,一个很简单的小练习,没想到发现了一个正则表达式匹配 IPv4 地址的坑,涉及到正则表达式的贪心特性以及首位子表达式优先。世事洞明皆学问,此言不虚,所以,还是记录一下吧。</p>]]></content>
<tags>
<tag> 基础 </tag>
<tag> RegEx </tag>
<tag> 正则表达式 </tag>
<tag> 贪心算法 </tag>
<tag> IPv4 </tag>
</tags>
</entry>
<entry>
<title>如何用 C 语言画点阵图</title>
<link href="2021/04/03/ru-he-yong-c-yu-yan-hua-dian-zhen-tu/"/>
<url>2021/04/03/ru-he-yong-c-yu-yan-hua-dian-zhen-tu/</url>
<content type="html"><![CDATA[<h1 id="1-如何用-c-语言画这个图"><a class="markdownIt-Anchor" href="#1-如何用-c-语言画这个图"></a> 1. 如何用 C 语言画这个图</h1><h2 id="11-原问题"><a class="markdownIt-Anchor" href="#11-原问题"></a> 1.1. 原问题</h2><blockquote><p>求大神这个图案怎么用 C 语言编写?</p></blockquote><pre class="line-numbers language-cmd" data-language="cmd"><code class="language-cmd"> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><span id="more"></span><p>我才不会给一个平凡解,给别人来交功课。希望可以用另类一些的方法去解决(至少不是老师要求的),可以让同学开拓一下思路。但这个原答案:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token function">main</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator"><</span><span class="token number">288</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token function">printf</span><span class="token punctuation">(</span>i<span class="token operator">%</span><span class="token number">24</span><span class="token operator">-</span><span class="token number">23</span><span class="token operator">?</span><span class="token function">abs</span><span class="token punctuation">(</span>i<span class="token operator">%</span><span class="token number">24</span><span class="token operator">-</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">24</span><span class="token operator">></span>i<span class="token operator">|</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token punctuation">(</span>i<span class="token operator">%</span><span class="token number">24</span><span class="token operator">+</span>i<span class="token operator">/</span><span class="token number">72</span><span class="token operator">*</span><span class="token number">3</span><span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">6</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">></span>i<span class="token operator">/</span><span class="token number">24</span><span class="token operator">%</span><span class="token number">3</span><span class="token operator">?</span><span class="token string">" "</span><span class="token operator">:</span><span class="token string">"* "</span><span class="token operator">:</span><span class="token string">"\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>是「压缩」后的结果,同学可能无法理解当中的意义。我尝试在本文简单解析一下。</p><h2 id="12-绘图框架"><a class="markdownIt-Anchor" href="#12-绘图框架"></a> 1.2. 绘图框架</h2><p>在计算机图形学中,要合成(<strong>synthesize</strong>)一个图像(image)时,通常有两种方法:</p><p>绘画每个形状,填充形状覆盖的像素。</p><p>对于图像中每个像素,采样(sample)该像素覆盖了什么形状。</p><p>第一种就是光栅化(<strong>rasterization</strong>)算法,第二种包括光线追踪(<strong>ray tracing</strong>)、光线步进(<strong>ray marching</strong>)等算法。</p><p>第二种做法可以理解为设计一个数学函数,例如二维的单色图像就可以定义为一个函数 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo>:</mo><msup><mi mathvariant="double-struck">R</mi><mn>2</mn></msup><mo>→</mo><mo stretchy="false">[</mo><mn>0</mn><mo separator="true">,</mo><mn>1</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">f : \mathbb{R}^2 \rightarrow [0,1]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">:</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord"><span class="mord mathbb">R</span></span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">→</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">0</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">1</span><span class="mclose">]</span></span></span></span>。这种方式可以用较少的代码画出复杂的形状。如果要输出文本模式,只用两个符号表示图形,可用这个代码框架:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><stdio.h></span></span><span class="token keyword">const</span> <span class="token keyword">int</span> w <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> h <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token comment">/*...*/</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> x<span class="token punctuation">,</span> y<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>y <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> y <span class="token operator"><</span> h<span class="token punctuation">;</span> y<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> w<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token function">f</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">"* "</span> <span class="token operator">:</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">puts</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>例如,圆盘(disk)在数学上可定义为一个隐函数 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>−</mo><msub><mi>c</mi><mi>x</mi></msub><msup><mo stretchy="false">)</mo><mn>2</mn></msup><mo>+</mo><mo stretchy="false">(</mo><mi>y</mi><mo>−</mo><msub><mi>c</mi><mi>y</mi></msub><msup><mo stretchy="false">)</mo><mn>2</mn></msup><mo>≤</mo><msup><mi>r</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">(x - c_x)^2 + (y - c_y)^2 \le r^2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathdefault">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1.064108em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathdefault">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathdefault mtight">x</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1.1002159999999999em;vertical-align:-0.286108em;"></span><span class="mord"><span class="mord mathdefault">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.15139200000000003em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathdefault mtight" style="margin-right:0.03588em;">y</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.286108em;"><span></span></span></span></span></span></span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">≤</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord mathdefault" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span></span></span></span>,那么画一个置于画布中心 (10, 10)、半径 8 的圆盘只需要定义 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo stretchy="false">(</mo><mi>x</mi><mo separator="true">,</mo><mi>y</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">f(x, y)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mord mathdefault">x</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span><span class="mclose">)</span></span></span></span> 为:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token punctuation">(</span>x <span class="token operator">-</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>x <span class="token operator">-</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token punctuation">(</span>y <span class="token operator">-</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>y <span class="token operator">-</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator"><=</span> <span class="token number">8</span> <span class="token operator">*</span> <span class="token number">8</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>输出:</p><pre class="line-numbers language-cmd" data-language="cmd"><code class="language-cmd"> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="121-直角等腰三角形"><a class="markdownIt-Anchor" href="#121-直角等腰三角形"></a> 1.2.1. 直角等腰三角形</h3><p>先考虑半个三角形,可用 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>x</mi><mo>≤</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">x \le y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7719400000000001em;vertical-align:-0.13597em;"></span><span class="mord mathdefault">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">≤</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span></span></span></span>:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> x <span class="token operator"><=</span> y<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>设 w = 23, h = 12,那么输出是这样的:</p><pre class="line-numbers language-cmd" data-language="cmd"><code class="language-cmd">* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>要画出题目中的大三角形,我们可以使用绝对值 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">∣</mo><mi>x</mi><mo>−</mo><msub><mi>c</mi><mi>x</mi></msub><mo stretchy="false">∣</mo><mo>≤</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">\lvert x - c_x\rvert \le y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">∣</span><span class="mord mathdefault">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathdefault">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathdefault mtight">x</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">∣</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">≤</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span></span></span></span>,<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msub><mi>c</mi><mi>x</mi></msub></mrow><annotation encoding="application/x-tex">c_x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathdefault">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathdefault mtight">x</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> 表示对称轴的 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">x</span></span></span></span> 坐标:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">abs</span><span class="token punctuation">(</span>x <span class="token operator">-</span> <span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator"><=</span> y<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>输出:</p><pre class="line-numbers language-cmd" data-language="cmd"><code class="language-cmd"> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="122-密铺三角形"><a class="markdownIt-Anchor" href="#122-密铺三角形"></a> 1.2.2. 密铺三角形</h3><p>然后,我们考虑要画一些密铺三角形。首先,我们可把上面画三角形的功能写成一个函数:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">triangle</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">abs</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token operator"><=</span> y<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>这个三角形的上顶点是位于 (0, 0) ,我们可以通过平移坐标来放置它在不同位置。另外,为了密辅,我们可用取模运算:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">triangle</span><span class="token punctuation">(</span>x <span class="token operator">%</span> <span class="token number">6</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">,</span> y <span class="token operator">%</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>输出:</p><pre class="line-numbers language-cmd" data-language="cmd"><code class="language-cmd"> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>但这种密辅和题目要求的有出入,我们要对每行三角形偏移半个三角形:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">triangle</span><span class="token punctuation">(</span><span class="token punctuation">(</span>x <span class="token operator">+</span> y <span class="token operator">/</span> <span class="token number">3</span> <span class="token operator">*</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">%</span> <span class="token number">6</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">,</span> y <span class="token operator">%</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>y</mi><mi mathvariant="normal">/</mi><mn>3</mn></mrow><annotation encoding="application/x-tex">y / 3</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span><span class="mord">/</span><span class="mord">3</span></span></span></span> 是三角形的行数,<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>y</mi><mi mathvariant="normal">/</mi><mn>3</mn><mo>∗</mo><mn>3</mn></mrow><annotation encoding="application/x-tex">y / 3 * 3</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span><span class="mord">/</span><span class="mord">3</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">∗</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">3</span></span></span></span> 是对每行三角形偏移半个三角形(包括一个空白后三角形总宽是6),然后再偏移半个三角形。输出:</p><pre class="line-numbers language-cmd" data-language="cmd"><code class="language-cmd"> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>可以看到中间的部分是我们所需的密铺三角形。</p><h3 id="123-交集"><a class="markdownIt-Anchor" href="#123-交集"></a> 1.2.3. 交集</h3><p>要结合上面的大三角形和密铺三角形,非常简单,只需用逻辑与就能实现两个图形的交集:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">triangle</span><span class="token punctuation">(</span>x <span class="token operator">-</span> <span class="token number">11</span><span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token function">triangle</span><span class="token punctuation">(</span><span class="token punctuation">(</span>x <span class="token operator">+</span> y <span class="token operator">/</span> <span class="token number">3</span> <span class="token operator">*</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">%</span> <span class="token number">6</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">,</span> y <span class="token operator">%</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>这里我们重复地使用 <strong>triangle</strong> 去画这两种三角形,输出:</p><pre class="line-numbers language-cmd" data-language="cmd"><code class="language-cmd"> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>到这一步,可算是大功告成。(唉⋯⋯几分钟能写完的算什么大功)</p><h2 id="13-压缩"><a class="markdownIt-Anchor" href="#13-压缩"></a> 1.3. 压缩</h2><ol><li><p>为了减少代码的字符,我们从两个 for 变成一个。然后置换 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>x</mi><mo>=</mo><mi>i</mi><mi mathvariant="normal">%</mi><mn>24</mn></mrow><annotation encoding="application/x-tex">x = i \% 24</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.80556em;vertical-align:-0.05556em;"></span><span class="mord mathdefault">i</span><span class="mord">%</span><span class="mord">2</span><span class="mord">4</span></span></span></span>, <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>y</mi><mo>=</mo><mi>i</mi><mi mathvariant="normal">/</mi><mn>24</mn></mrow><annotation encoding="application/x-tex">y = i / 24</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault">i</span><span class="mord">/</span><span class="mord">2</span><span class="mord">4</span></span></span></span>。</p></li><li><p>把每行最后一个字符用作换行。所以宽度从 23 改为 24。</p></li><li><p>因为 <code>main</code> 的第一个参数是 <code>int</code>,可用来声明那个 <code>for</code> 变量。</p></li></ol><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token function">main</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">24</span> <span class="token operator">*</span> <span class="token number">12</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span>i <span class="token operator">%</span> <span class="token number">24</span> <span class="token operator">!=</span> <span class="token number">23</span> <span class="token operator">?</span> <span class="token function">f</span><span class="token punctuation">(</span>i <span class="token operator">%</span> <span class="token number">24</span><span class="token punctuation">,</span> i <span class="token operator">/</span> <span class="token number">24</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">"* "</span> <span class="token operator">:</span> <span class="token string">" "</span><span class="token operator">:</span><span class="token string">"\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><ol start="4"><li>人手内联 <code>triangle()</code>。</li></ol><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">abs</span><span class="token punctuation">(</span>x <span class="token operator">-</span> <span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator"><=</span> y <span class="token operator">&&</span> <span class="token function">abs</span><span class="token punctuation">(</span><span class="token punctuation">(</span>x <span class="token operator">+</span> y <span class="token operator">/</span> <span class="token number">3</span> <span class="token operator">*</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">%</span> <span class="token number">6</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator"><=</span> y <span class="token operator">%</span> <span class="token number">3</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><ol start="5"><li><p>人手内联 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">f()</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mclose">)</span></span></span></span>。<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>i</mi><mi mathvariant="normal">/</mi><mn>24</mn><mi mathvariant="normal">/</mi><mn>3</mn></mrow><annotation encoding="application/x-tex">i / 24 / 3</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault">i</span><span class="mord">/</span><span class="mord">2</span><span class="mord">4</span><span class="mord">/</span><span class="mord">3</span></span></span></span> 等价于 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>i</mi><mi mathvariant="normal">/</mi><mn>72</mn></mrow><annotation encoding="application/x-tex">i / 72</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault">i</span><span class="mord">/</span><span class="mord">7</span><span class="mord">2</span></span></span></span>。</p></li><li><p>移除所有非必要的空白符。</p></li></ol><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token function">main</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator"><</span><span class="token number">288</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token function">printf</span><span class="token punctuation">(</span>i<span class="token operator">%</span><span class="token number">24</span><span class="token operator">!=</span><span class="token number">23</span><span class="token operator">?</span><span class="token function">abs</span><span class="token punctuation">(</span>i<span class="token operator">%</span><span class="token number">24</span><span class="token operator">-</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token operator"><=</span>i<span class="token operator">/</span><span class="token number">24</span><span class="token operator">&&</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token punctuation">(</span>i<span class="token operator">%</span><span class="token number">24</span><span class="token operator">+</span>i<span class="token operator">/</span><span class="token number">72</span><span class="token operator">*</span><span class="token number">3</span><span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">6</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator"><=</span>i<span class="token operator">/</span><span class="token number">24</span><span class="token operator">%</span><span class="token number">3</span><span class="token operator">?</span><span class="token string">"* "</span><span class="token operator">:</span><span class="token string">" "</span><span class="token operator">:</span><span class="token string">"\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ol start="7"><li><p>把 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">&</mi><mi mathvariant="normal">&</mi></mrow><annotation encoding="application/x-tex">\&\&</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord">&</span><span class="mord">&</span></span></span></span> 改成 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">&</mi></mrow><annotation encoding="application/x-tex">\&</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord">&</span></span></span></span>,因去除了短路求值降低了性能,但省了一个字符。</p></li><li><p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>i</mi><mi mathvariant="normal">%</mi><mn>24</mn><mo stretchy="false">!</mo><mo>=</mo><mn>23</mn></mrow><annotation encoding="application/x-tex">i \% 24 != 23</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.80556em;vertical-align:-0.05556em;"></span><span class="mord mathdefault">i</span><span class="mord">%</span><span class="mord">2</span><span class="mord">4</span><span class="mclose">!</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">2</span><span class="mord">3</span></span></span></span> 等价于 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>i</mi><mi mathvariant="normal">%</mi><mn>24</mn><mo>−</mo><mn>23</mn></mrow><annotation encoding="application/x-tex">i \% 24 - 23</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.83333em;vertical-align:-0.08333em;"></span><span class="mord mathdefault">i</span><span class="mord">%</span><span class="mord">2</span><span class="mord">4</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">2</span><span class="mord">3</span></span></span></span>,省了一个字符。</p></li><li><p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>a</mi><mi>b</mi><mi>s</mi><mo stretchy="false">(</mo><mi>i</mi><mi mathvariant="normal">%</mi><mn>24</mn><mo>−</mo><mn>11</mn><mo stretchy="false">)</mo><mo>≤</mo><mi>i</mi><mi mathvariant="normal">/</mi><mn>24</mn></mrow><annotation encoding="application/x-tex">abs(i \% 24 - 11) \le i / 24</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault">a</span><span class="mord mathdefault">b</span><span class="mord mathdefault">s</span><span class="mopen">(</span><span class="mord mathdefault">i</span><span class="mord">%</span><span class="mord">2</span><span class="mord">4</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">1</span><span class="mord">1</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">≤</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault">i</span><span class="mord">/</span><span class="mord">2</span><span class="mord">4</span></span></span></span> 可改成 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>a</mi><mi>b</mi><mi>s</mi><mo stretchy="false">(</mo><mi>i</mi><mi mathvariant="normal">%</mi><mn>24</mn><mo>−</mo><mn>11</mn><mo stretchy="false">)</mo><mo>∗</mo><mn>24</mn><mo>≤</mo><mi>i</mi></mrow><annotation encoding="application/x-tex">abs(i \% 24 - 11) * 24 \le i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault">a</span><span class="mord mathdefault">b</span><span class="mord mathdefault">s</span><span class="mopen">(</span><span class="mord mathdefault">i</span><span class="mord">%</span><span class="mord">2</span><span class="mord">4</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">1</span><span class="mord">1</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">∗</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.78041em;vertical-align:-0.13597em;"></span><span class="mord">2</span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">≤</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.65952em;vertical-align:0em;"></span><span class="mord mathdefault">i</span></span></span></span>,提高性能。</p></li><li><p>把条件取反,交换后面的字符串,即 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">c</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mo stretchy="false">?</mo><mi mathvariant="normal">a</mi><mo>:</mo><mi mathvariant="normal">b</mi></mrow><annotation encoding="application/x-tex">\mathrm{cond ? a : b}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord"><span class="mord mathrm">c</span><span class="mord mathrm">o</span><span class="mord mathrm">n</span><span class="mord mathrm">d</span><span class="mclose">?</span><span class="mord mathrm">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">:</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mord mathrm">b</span></span></span></span></span> 等价于 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">!</mo><mi mathvariant="normal">c</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mo stretchy="false">?</mo><mi mathvariant="normal">b</mi><mo>:</mo><mi mathvariant="normal">a</mi></mrow><annotation encoding="application/x-tex">\mathrm{!cond ? b : a}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord"><span class="mclose">!</span><span class="mord mathrm">c</span><span class="mord mathrm">o</span><span class="mord mathrm">n</span><span class="mord mathrm">d</span><span class="mclose">?</span><span class="mord mathrm">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">:</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mord mathrm">a</span></span></span></span></span>。然后德摩根定律 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">!</mo><mo stretchy="false">(</mo><mi mathvariant="normal">c</mi><mi mathvariant="normal">&</mi><mi mathvariant="normal">&</mi><mi mathvariant="normal">d</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\mathrm{!(c \&\& d)}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mclose">!</span><span class="mopen">(</span><span class="mord mathrm">c</span><span class="mord mathrm">&</span><span class="mord mathrm">&</span><span class="mord mathrm">d</span><span class="mclose">)</span></span></span></span></span> 等价于<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">(</mo><mo stretchy="false">!</mo><mi mathvariant="normal">c</mi><mo stretchy="false">)</mo><mi mathvariant="normal">∣</mi><mi mathvariant="normal">∣</mi><mo stretchy="false">(</mo><mo stretchy="false">!</mo><mi mathvariant="normal">d</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\mathrm{(!c) || (!d)}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mopen">(</span><span class="mclose">!</span><span class="mord mathrm">c</span><span class="mclose">)</span><span class="mord mathrm">∣</span><span class="mord mathrm">∣</span><span class="mopen">(</span><span class="mclose">!</span><span class="mord mathrm">d</span><span class="mclose">)</span></span></span></span></span>,所以<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>≤</mo></mrow><annotation encoding="application/x-tex">\le</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7719400000000001em;vertical-align:-0.13597em;"></span><span class="mrel">≤</span></span></span></span> 变成 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>></mo></mrow><annotation encoding="application/x-tex">></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mrel">></span></span></span></span>,<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">&</mi></mrow><annotation encoding="application/x-tex">\&</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord">&</span></span></span></span> 变成 <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">∣</mi></mrow><annotation encoding="application/x-tex">|</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">∣</span></span></span></span>。省了 2 个字符。</p></li></ol><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token function">main</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator"><</span><span class="token number">288</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token function">printf</span><span class="token punctuation">(</span>i<span class="token operator">%</span><span class="token number">24</span><span class="token operator">-</span><span class="token number">23</span><span class="token operator">?</span><span class="token function">abs</span><span class="token punctuation">(</span>i<span class="token operator">%</span><span class="token number">24</span><span class="token operator">-</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">24</span><span class="token operator">></span>i<span class="token operator">|</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token punctuation">(</span>i<span class="token operator">%</span><span class="token number">24</span><span class="token operator">+</span>i<span class="token operator">/</span><span class="token number">72</span><span class="token operator">*</span><span class="token number">3</span><span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">6</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">></span>i<span class="token operator">/</span><span class="token number">24</span><span class="token operator">%</span><span class="token number">3</span><span class="token operator">?</span><span class="token string">" "</span><span class="token operator">:</span><span class="token string">"* "</span><span class="token operator">:</span><span class="token string">"\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>此文的前半步部分可了解一下思路,这个压缩部分纯粹娱乐,不要写这样的代码。</p><h2 id="14-相关问题"><a class="markdownIt-Anchor" href="#14-相关问题"></a> 1.4. 相关问题</h2><ul><li><p><a href="https://www.zhihu.com/question/52188822/answer/129373798">C语言如何用for循环完成输出下列图形?</a></p></li><li><p><a href="https://www.zhihu.com/question/20187195/answer/34873279">如何用C语言画一个“心形”?</a></p></li><li><p><a href="https://www.zhihu.com/question/27015321/answer/35028446">如何用 C 语言画一个“圣诞树”?</a></p></li></ul><hr><p>编辑于 2016-11-16</p>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> 算法 </tag>
<tag> C 语言 </tag>
<tag> ASCII </tag>
</tags>
</entry>
<entry>
<title>Markdown 教程</title>
<link href="2021/04/03/markdown-jiao-cheng/"/>
<url>2021/04/03/markdown-jiao-cheng/</url>
<content type="html"><![CDATA[<h1 id="markdown-基本要素"><a class="markdownIt-Anchor" href="#markdown-基本要素"></a> Markdown 基本要素</h1><p>这篇文件意在简要介绍 GitHub Flavored Markdown 写作。</p><p>什么是 Markdown?</p><p>Markdown 是一种文本格式。你可以用它来控制文档的显示。使用 markdown,你可以创建粗体的文字,斜体的文字,添加图片,并且创建列表 等等。基本上来讲,Markdown 就是普通的文字加上 # 或者 * 等符号。</p><span id="more"></span><h2 id="语法说明"><a class="markdownIt-Anchor" href="#语法说明"></a> 语法说明</h2><h3 id="标题"><a class="markdownIt-Anchor" href="#标题"></a> 标题</h3><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token title important"><span class="token punctuation">#</span> 这是 <h1> 一级标题</span><span class="token title important"><span class="token punctuation">##</span> 这是 <h2> 二级标题</span><span class="token title important"><span class="token punctuation">###</span> 这是 <h3> 三级标题</span><span class="token title important"><span class="token punctuation">####</span> 这是 <h4> 四级标题</span><span class="token title important"><span class="token punctuation">#####</span> 这是 <h5> 五级标题</span><span class="token title important"><span class="token punctuation">######</span> 这是 <h6> 六级标题</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="强调"><a class="markdownIt-Anchor" href="#强调"></a> 强调</h3><h4 id="斜体"><a class="markdownIt-Anchor" href="#斜体"></a> 斜体</h4><ul><li>Markdown 源码</li></ul><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token italic"><span class="token punctuation">*</span><span class="token content">这会是 斜体 的文字</span><span class="token punctuation">*</span></span><span class="token italic"><span class="token punctuation">_</span><span class="token content">这会是 斜体 的文字</span><span class="token punctuation">_</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><ul><li>渲染效果<br><em>这会是 斜体 的文字</em><br><em>这会是 斜体 的文字</em></li></ul><h4 id="粗体"><a class="markdownIt-Anchor" href="#粗体"></a> 粗体</h4><ul><li>Markdown 源码</li><li></li></ul><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token bold"><span class="token punctuation">**</span><span class="token content">这会是 粗体 的文字</span><span class="token punctuation">**</span></span><span class="token bold"><span class="token punctuation">__</span><span class="token content">这会是 粗体 的文字</span><span class="token punctuation">__</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><ul><li>渲染效果<br><strong>这会是 粗体 的文字</strong><br><strong>这会是 粗体 的文字</strong></li></ul><h4 id="组合体"><a class="markdownIt-Anchor" href="#组合体"></a> 组合体</h4><p>Markdown 源码</p><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token italic"><span class="token punctuation">_</span><span class="token content">你也 <span class="token bold"><span class="token punctuation">**</span><span class="token content">组合</span><span class="token punctuation">**</span></span> 这些符号</span><span class="token punctuation">_</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>渲染效果<br><em>你也 <strong>组合</strong> 这些符号</em></li></ul><h4 id="删除线"><a class="markdownIt-Anchor" href="#删除线"></a> 删除线</h4><p>Markdown 源码</p><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token strike"><span class="token punctuation">~~</span><span class="token content">这个文字将会被横线删除</span><span class="token punctuation">~~</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>渲染效果<br><s>这个文字将会被横线删除</s></li></ul><h3 id="列表"><a class="markdownIt-Anchor" href="#列表"></a> 列表</h3><h4 id="无序列表"><a class="markdownIt-Anchor" href="#无序列表"></a> 无序列表</h4><p>Markdown 源码</p><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token list punctuation">-</span> 项目 1<span class="token list punctuation">-</span> 项目 2 <span class="token list punctuation">-</span> 项目 2a <span class="token list punctuation">-</span> 项目 2b<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><ul><li><p>渲染效果</p></li><li><p>项目 1</p></li><li><p>项目 2</p><ul><li>项目 2a</li><li>项目 2b</li></ul></li></ul><h4 id="有序列表"><a class="markdownIt-Anchor" href="#有序列表"></a> 有序列表</h4><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token list punctuation">1.</span> 项目 1<span class="token list punctuation">2.</span> 项目 2<span class="token list punctuation">3.</span> 项目 3 <span class="token list punctuation">1.</span> 项目 3a <span class="token list punctuation">2.</span> 项目 3b<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>渲染效果</li></ul><ol><li>Item 1</li><li>Item 2</li><li>Item 3<ol><li>Item 3a</li><li>Item 3b</li></ol></li></ol><h3 id="添加图片"><a class="markdownIt-Anchor" href="#添加图片"></a> 添加图片</h3><ul><li>Markdown 源码</li></ul><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token url"><span class="token operator">!</span>[<span class="token content">GitHub Logo</span>](<span class="token url">/images/logo.png</span> <span class="token string">"description"</span>)</span>Format: <span class="token url"><span class="token operator">!</span>[<span class="token content">Alt Text</span>](<span class="token url">url</span> <span class="token string">"description"</span>)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><ul><li>渲染效果<br>百度:<img src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" alt="百度" title="baidu.com"></li></ul><h3 id="链接"><a class="markdownIt-Anchor" href="#链接"></a> 链接</h3><ul><li>Markdown 源码</li></ul><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown">https://github.com - 自动生成!<span class="token url">[<span class="token content">GitHub</span>](<span class="token url">https://github.com</span>)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><ul><li>渲染效果</li></ul><p>百度:<a href="https://www.baidu.com">百度</a></p><h3 id="引用"><a class="markdownIt-Anchor" href="#引用"></a> 引用</h3><p>正如 Kanye West 所说:</p><blockquote><p>We’re living the future so<br>the present is our past.</p></blockquote><h3 id="分割线"><a class="markdownIt-Anchor" href="#分割线"></a> 分割线</h3><p>如下,三个或者更多的</p><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token title important">连字符<span class="token punctuation">---</span></span>星号<span class="token hr punctuation">***</span>下划线___<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>渲染效果</li></ul><hr><h3 id="行内代码"><a class="markdownIt-Anchor" href="#行内代码"></a> 行内代码</h3><p>我觉得你应该在这里使用 <code><addr></code> 才对。</p><h3 id="代码块"><a class="markdownIt-Anchor" href="#代码块"></a> 代码块</h3><p>你可以在你的代码上面和下面添加 <code>```</code> 来表示代码块。</p><h3 id="语法高亮"><a class="markdownIt-Anchor" href="#语法高亮"></a> 语法高亮</h3><p>你可以给你的代码块添加任何一种语言的语法高亮</p><p>例如,给 <code>ruby</code> 代码添加语法高亮:</p><pre class="line-numbers language-ruby" data-language="ruby"><code class="language-ruby"><span class="token keyword">require</span> <span class="token string">'redcarpet'</span>markdown <span class="token operator">=</span> <span class="token constant">Redcarpet</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span><span class="token string">"Hello World!"</span><span class="token punctuation">)</span>puts markdown<span class="token punctuation">.</span>to_html<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h3 id="任务列表"><a class="markdownIt-Anchor" href="#任务列表"></a> 任务列表</h3><ul><li>Markdown 源码</li></ul><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token list punctuation">-</span> [x] @mentions, #refs, [links](), <span class="token bold"><span class="token punctuation">**</span><span class="token content">formatting</span><span class="token punctuation">**</span></span>, and <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>del</span><span class="token punctuation">></span></span>tags<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>del</span><span class="token punctuation">></span></span> supported<span class="token list punctuation">-</span> [x] list syntax required (any unordered or ordered list supported)<span class="token list punctuation">-</span> [x] this is a complete item<span class="token list punctuation">-</span> [ ] this is an incomplete item<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><ul><li>渲染效果</li></ul><ul><li>[x] @mentions, #refs, <a href="">links</a>, <strong>formatting</strong>, and <del>tags</del> supported</li><li>[x] list syntax required (any unordered or ordered list supported)</li><li>[x] this is a complete item</li><li>[ ] this is an incomplete item</li></ul><h3 id="表格"><a class="markdownIt-Anchor" href="#表格"></a> 表格</h3><ul><li>Markdown 源码</li></ul><pre class="line-numbers language-markdown" data-language="markdown"><code class="language-markdown"><span class="token table"><span class="token table-header-row"><span class="token table-header important">First Header </span><span class="token punctuation">|</span><span class="token table-header important"> Second Header</span></span><span class="token table-line"><span class="token punctuation">------------</span> <span class="token punctuation">|</span> <span class="token punctuation">-------------</span></span><span class="token table-data-rows"><span class="token table-data">Content from cell 1 </span><span class="token punctuation">|</span><span class="token table-data"> Content from cell 2</span><span class="token table-data">Content in the first column </span><span class="token punctuation">|</span><span class="token table-data"> Content in the second column</span></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><ul><li>渲染效果</li></ul><table><thead><tr><th>First Header</th><th>Second Header</th></tr></thead><tbody><tr><td>Content from cell 1</td><td>Content from cell 2</td></tr><tr><td>Content in the first column</td><td>Content in the second column</td></tr></tbody></table>]]></content>
<categories>
<category> Markdown </category>
</categories>
<tags>
<tag> Markdown </tag>
<tag> 基础 </tag>
<tag> 教程 </tag>
</tags>
</entry>
<entry>
<title>Hello World</title>
<link href="2021/04/02/hello-world/"/>
<url>2021/04/02/hello-world/</url>
<content type="html"><![CDATA[<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p><h2 id="quick-start"><a class="markdownIt-Anchor" href="#quick-start"></a> Quick Start</h2><h3 id="create-a-new-post"><a class="markdownIt-Anchor" href="#create-a-new-post"></a> Create a new post</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo new <span class="token string">"My New Post"</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p><h3 id="run-server"><a class="markdownIt-Anchor" href="#run-server"></a> Run server</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo server<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p><h3 id="generate-static-files"><a class="markdownIt-Anchor" href="#generate-static-files"></a> Generate static files</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo generate<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p><h3 id="deploy-to-remote-sites"><a class="markdownIt-Anchor" href="#deploy-to-remote-sites"></a> Deploy to remote sites</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo deploy<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>]]></content>
</entry>
</search>