-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
233 lines (218 loc) · 17.5 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>Codewars|Highest and Lowest</title>
<url>/Highest%20and%20Lowest/</url>
<content><![CDATA[<h2 id="題目網址"><a href="#題目網址" class="headerlink" title="題目網址"></a>題目網址</h2><p><a href="https://www.codewars.com/kata/554b4ac871d6813a03000035/train/python">https://www.codewars.com/kata/554b4ac871d6813a03000035/train/python</a></p>
<div class="note info"><p><strong>資訊</strong><br>本文使用 <strong>Python</strong> 解題</p>
</div>
<h2 id="程式碼"><a href="#程式碼" class="headerlink" title="程式碼"></a>程式碼</h2><span id="more"></span>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment"># *-----------------*</span></span><br><span class="line"><span class="comment"># | 棕 熊 |</span></span><br><span class="line"><span class="comment"># *-----------------*</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">high_and_low</span>(<span class="params">numbers</span>):</span><br><span class="line"> a=<span class="built_in">list</span>(<span class="built_in">map</span>(<span class="built_in">int</span>,numbers.split(<span class="string">" "</span>)))</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">str</span>(<span class="built_in">max</span>(a))+<span class="string">" "</span>+<span class="built_in">str</span>(<span class="built_in">min</span>(a))</span><br></pre></td></tr></table></figure>
<h2 id="思路/想法"><a href="#思路/想法" class="headerlink" title="思路/想法"></a>思路/想法</h2><p>簡單的<code>min()</code>、<code>max()</code>函數應用。</p>
]]></content>
<categories>
<category>Codewars</category>
<category>7kyu</category>
<category>基礎函數應用</category>
</categories>
</entry>
<entry>
<title>ZeroJudge|a012. 10055 - Hashmat the Brave Warrior</title>
<url>/a012/</url>
<content><![CDATA[<h2 id="題目網址"><a href="#題目網址" class="headerlink" title="題目網址"></a><strong>題目網址</strong></h2><p><a href="https://zerojudge.tw/ShowProblem?problemid=a012">https://zerojudge.tw/ShowProblem?problemid=a012</a></p>
<div class="note info"><p><strong>資訊</strong><br>本文使用 <strong>C++</strong> 解題</p>
</div>
<h2 id="提示"><a href="#提示" class="headerlink" title="提示"></a><strong>提示</strong></h2><span id="more"></span>
<!-- <p>
<span class="info-container">
<span class="info-icon">ⓘ</span>
<span class="tooltip">將滑鼠懸停在黑色遮蔽條上以查看遮蓋住的提示文字!</span>
</span></p> -->
<ol>
<li>本題要求輸出雙方士兵的數量差,而這個數必須是正數,所以你也許會需要用到數學上的<span class="spoiler">絕對值</span>?</li>
<li>題目提到Hashmat及敵人的士兵數不會超過2^63,但人數有可能是2^62或之類的,而這麼大的數字,也許需要使用<span class="spoiler">long long int</span>?</li>
<li>從範例輸入/輸出來看,這題也許是直到EOF為止?</li>
</ol>
<h2 id="程式碼"><a href="#程式碼" class="headerlink" title="程式碼"></a><strong>程式碼</strong></h2><div class="tabs" id="程式碼"><ul class="nav-tabs"><li class="tab active"><a href="#程式碼-1">使用<mark class="label info">abs()</mark></a></li><li class="tab"><a href="#程式碼-2">使用條件運算子<mark class="label success">?:</mark></a></li></ul><div class="tab-content"><div class="tab-pane active" id="程式碼-1"><figure class="highlight cpp"><figcaption><span>a012. 10055 - Hashmat the Brave Warrior</span></figcaption><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><cmath></span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="type">long</span> <span class="type">long</span> <span class="type">int</span> n1,n2;</span><br><span class="line"> <span class="keyword">while</span>(cin>>n1>>n2)</span><br><span class="line"> cout<<<span class="built_in">abs</span>(n1-n2)<<endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="程式碼-2"><figure class="highlight cpp"><figcaption><span>a012. 10055 - Hashmat the Brave Warrior</span></figcaption><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><cmath></span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="type">long</span> <span class="type">long</span> <span class="type">int</span> n1,n2;</span><br><span class="line"> <span class="keyword">while</span>(cin>>n1>>n2)</span><br><span class="line"> n1>n2?cout<<n1-n2<<endl:cout<<n2-n1<<endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></div></div></div>
<h2 id="思路/想法"><a href="#思路/想法" class="headerlink" title="思路/想法"></a>思路/想法</h2><p>推測本題是直到EOF為止,因此使用abs()版本程式碼跟條件運算子<code>?:</code>版本的第8行使用<code>while(cin>>n1>>n2)</code>。</p>
<h3 id="函數abs"><a href="#函數abs" class="headerlink" title="函數abs()"></a>函數<code>abs()</code></h3><p><code>abs()</code>是用來取<strong>絕對值</strong>的函數,使用時應引入<code><cmath></code>函式庫。<br>如果要輸出a-b的絕對值(也就是|a-b|),程式碼如下:</p>
<figure class="highlight cpp"><figcaption><span>abs()示例</span></figcaption><table><tr><td class="code"><pre><span class="line">cout<<<span class="built_in">abs</span>(a-b)<<endl;</span><br></pre></td></tr></table></figure>
<h3 id="條件運算子"><a href="#條件運算子" class="headerlink" title="條件運算子?:"></a>條件運算子<code>?:</code></h3><p><code>?:</code>的使用方式如下:<code><條件式>?<True表達式>:<False表達式></code>。<br>來看看今天的程式碼:</p>
<figure class="highlight cpp"><figcaption><span>條件運算子示例</span></figcaption><table><tr><td class="code"><pre><span class="line">n1>n2?cout<<n1-n2<<endl:cout<<n2-n1<<endl;</span><br></pre></td></tr></table></figure>
<p>他的意思其實就是:n1有大於n2嗎?<strong>有</strong>的話,就輸出<code>n1-n2</code>的結果;<strong>否則</strong>輸出<code>n2-n1</code>的結果。</p>
<style>
.spoiler {
background-color: black;
color: black;
cursor: help;
}
.spoiler:hover {
color: white;
}
</style>
<style>
/* 基本樣式 */
.info-container {
display: inline-block;
position: relative;
cursor: pointer;
font-size: 16px;
}
/* 提示圖示樣式(黑白主題) */
.info-icon {
display: inline-block;
width: 18px;
height: 18px;
border-radius: 50%;
background-color: #000; /* 黑色背景 */
color: #fff; /* 白色文字 */
text-align: center;
line-height: 18px;
font-size: 14px;
font-weight: bold;
margin-left: 5px;
font-family: Arial, sans-serif;
}
/* 提示文字區塊(保持不變) */
.tooltip {
position: absolute;
bottom: 120%;
left: 50%;
transform: translateX(-50%);
background-color: #333;
color: #fff;
padding: 8px 12px;
border-radius: 4px;
font-size: 14px;
white-space: nowrap;
opacity: 0;
visibility: hidden;
transition: opacity 0.2s ease, visibility 0.2s ease;
z-index: 1000;
}
.tooltip::after {
content: '';
position: absolute;
top: 100%;
left: 50%;
transform: translateX(-50%);
border-width: 5px;
border-style: solid;
border-color: #333 transparent transparent transparent;
}
/* 滑鼠懸停時顯示提示文字 */
.info-container:hover .tooltip {
opacity: 1;
visibility: visible;
}
</style>
]]></content>
<categories>
<category>ZeroJudge</category>
<category>UVa題庫</category>
<category>UVa</category>
<category>CPE</category>
<category>一顆星</category>
<category>一顆星選集</category>
</categories>
<tags>
<tag>CPE</tag>
</tags>
</entry>
<entry>
<title>ZeroJudge|c813. 11332 - Summing Digits</title>
<url>/c813/</url>
<content><![CDATA[<h2 id="題目網址"><a href="#題目網址" class="headerlink" title="題目網址"></a>題目網址</h2><p><a href="https://zerojudge.tw/ShowProblem?problemid=c813">https://zerojudge.tw/ShowProblem?problemid=c813</a></p>
<div class="note info"><p><strong>資訊</strong><br>本文使用 <strong>C++</strong> 解題</p>
</div>
<h2 id="提示"><a href="#提示" class="headerlink" title="提示"></a>提示</h2><span id="more"></span>
<ol>
<li>題目提到「輸入是以0值做為結束,該值不需要輸出」,所以應該需要判斷輸入的數n是否為0,如果是的話,就跳出迴圈?</li>
</ol>
<h2 id="程式碼"><a href="#程式碼" class="headerlink" title="程式碼"></a>程式碼</h2><figure class="highlight cpp"><figcaption><span>c813. 11332 - Summing Digits</span></figcaption><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">theFormula</span><span class="params">(<span class="type">int</span> n)</span></span>{</span><br><span class="line"> <span class="type">int</span> sumOfDigit=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(n><span class="number">0</span>){</span><br><span class="line"> sumOfDigit+=(n%<span class="number">10</span>);</span><br><span class="line"> n/=<span class="number">10</span>;</span><br><span class="line"> } </span><br><span class="line"> <span class="keyword">if</span>(sumOfDigit<<span class="number">10</span>)</span><br><span class="line"> <span class="keyword">return</span> sumOfDigit;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">theFormula</span>(sumOfDigit);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="type">int</span> n; </span><br><span class="line"> <span class="keyword">while</span>(cin>>n&&n!=<span class="number">0</span>){</span><br><span class="line"> <span class="type">int</span> result=<span class="built_in">theFormula</span>(n);</span><br><span class="line"> cout<<result<<endl; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="思路/想法"><a href="#思路/想法" class="headerlink" title="思路/想法"></a>思路/想法</h2><p>首先,從題意我們可以看出,題目所要求的是各個位數的數字總和。<br>依照要求,我們可以寫出一個函數:利用取除以10的餘數,來得到每一位數字。依照這個邏輯,可以寫成下列函數:</p>
<figure class="highlight cpp"><figcaption><span>程式碼B1 自訂函數求所有位數總和</span></figcaption><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">theFormula</span><span class="params">(<span class="type">int</span> n)</span></span>{</span><br><span class="line"> <span class="type">int</span> sumOfDigit=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(n><span class="number">0</span>){</span><br><span class="line"> sumOfDigit+=(n%<span class="number">10</span>);</span><br><span class="line"> n/=<span class="number">10</span>;</span><br><span class="line"> } </span><br><span class="line"> <span class="keyword">if</span>(sumOfDigit<<span class="number">10</span>)</span><br><span class="line"> <span class="keyword">return</span> sumOfDigit;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">theFormula</span>(sumOfDigit);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>在以上程式碼的第3~6行,就是在執行求所有位數總和。在B1的第4行中,我們把各位數與<mark class="label warning">sumOfDigit</mark>的值相加;第5行的作用則是將已經相加過的那一位給去除,這樣才能繼續求第二位數字與<mark class="label warning">sumOfDigit</mark>的和。而7~10行的意義則是判斷<mark class="label warning">sumOfDigit</mark>是否是一位數,如果<strong>是</strong>,那就return <mark class="label warning">sumOfDigit</mark>的值;如果<strong>不是</strong>的話,就再執行一次<mark class="label info">theFormula</mark>,不過這次是使用<mark class="label warning">sumOfDigit</mark>來執行,也就是說,輸入<mark class="label info">theFormula</mark>函數的值n,就是<mark class="label warning">sumOfDigit</mark>。</p>
<figure class="highlight cpp"><figcaption><span>程式碼B2 主函式main()</span></figcaption><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="type">long</span> <span class="type">int</span> n; </span><br><span class="line"> <span class="keyword">while</span>(cin>>n&&n!=<span class="number">0</span>){</span><br><span class="line"> <span class="type">int</span> result=<span class="built_in">theFormula</span>(n);</span><br><span class="line"> cout<<result<<endl; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>主函式的第三行有一個while迴圈,其條件式<code>cin>>n&&n!=0</code>就是讓程式可以輸入多個資料n,直到輸入值為0為止。</p>
<style>
.spoiler {
background-color: black;
color: black;
cursor: help;
}
.spoiler:hover {
color: white;
}
</style>
<style>
/* 基本樣式 */
.info-container {
display: inline-block;
position: relative;
cursor: pointer;
font-size: 16px;
}
/* 提示圖示樣式(黑白主題) */
.info-icon {
display: inline-block;
width: 18px;
height: 18px;
border-radius: 50%;
background-color: #000; /* 黑色背景 */
color: #fff; /* 白色文字 */
text-align: center;
line-height: 18px;
font-size: 14px;
font-weight: bold;
margin-left: 5px;
font-family: Arial, sans-serif;
}
/* 提示文字區塊(保持不變) */
.tooltip {
position: absolute;
bottom: 120%;
left: 50%;
transform: translateX(-50%);
background-color: #333;
color: #fff;
padding: 8px 12px;
border-radius: 4px;
font-size: 14px;
white-space: nowrap;
opacity: 0;
visibility: hidden;
transition: opacity 0.2s ease, visibility 0.2s ease;
z-index: 1000;
}
.tooltip::after {
content: '';
position: absolute;
top: 100%;
left: 50%;
transform: translateX(-50%);
border-width: 5px;
border-style: solid;
border-color: #333 transparent transparent transparent;
}
/* 滑鼠懸停時顯示提示文字 */
.info-container:hover .tooltip {
opacity: 1;
visibility: visible;
}
</style>
]]></content>
<categories>
<category>ZeroJudge</category>
<category>UVa題庫</category>
<category>UVa</category>
<category>CPE</category>
<category>一顆星</category>
<category>一顆星選集</category>
</categories>
<tags>
<tag>CPE</tag>
<tag>字元與字串</tag>
</tags>
</entry>
</search>