<style>
html, body, .ui-content {
background-color: #333;
color: #ddd;
}
body > .ui-infobar {
display: none;
}
.ui-view-area > .ui-infobar {
display: block;
}
.markdown-body h1{
color: #9CCEF2;
}
.markdown-body h2,
.markdown-body h3{
color: #B1D6CA;
}
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
color: #ddd;
}
.markdown-body h1,
.markdown-body h2 {
border-bottom-color: #ffffff69;
}
.markdown-body h1 .octicon-link,
.markdown-body h2 .octicon-link,
.markdown-body h3 .octicon-link,
.markdown-body h4 .octicon-link,
.markdown-body h5 .octicon-link,
.markdown-body h6 .octicon-link {
color: #fff;
}
.markdown-body img {
background-color: transparent;
}
.ui-toc-dropdown .nav>.active:focus>a, .ui-toc-dropdown .nav>.active:hover>a, .ui-toc-dropdown .nav>.active>a {
color: white;
border-left: 2px solid white;
}
.expand-toggle:hover,
.expand-toggle:focus,
.back-to-top:hover,
.back-to-top:focus,
.go-to-bottom:hover,
.go-to-bottom:focus {
color: white;
}
.ui-toc-dropdown {
background-color: #333;
}
.ui-toc-label.btn {
background-color: #191919;
color: white;
}
.ui-toc-dropdown .nav>li>a:focus,
.ui-toc-dropdown .nav>li>a:hover {
color: white;
border-left: 1px solid white;
}
.markdown-body blockquote {
color: #bcbcbc;
}
.markdown-body table tr {
background-color: #5f5f5f;
}
.markdown-body table tr:nth-child(2n) {
background-color: #4f4f4f;
}
.markdown-body code,
.markdown-body tt {
color: #eee;
background-color: rgba(230, 230, 230, 0.36);
}
a,
.open-files-container li.selected a {
color: #5EB7E0;
}
</style>
###### tags: `tgirc早修book`
# Ch.03 題目練習
## while 迴圈
<font color="FEA0A0">**題目**</font>
1. [Zerojudge a004: 文文的求婚](https://zerojudge.tw/ShowProblem?problemid=a004)
2. [Zerojudge a024: 最大公因數(GCD)](https://zerojudge.tw/ShowProblem?problemid=a024)
3. [Zerojudge a010: 因數分解](https://zerojudge.tw/ShowProblem?problemid=a010)
4. [Zerojudge a005: Eva 的回家作業](https://zerojudge.tw/ShowProblem?problemid=a005)
5. [Zerojudge b572: 忘了東西的傑克](https://zerojudge.tw/ShowProblem?problemid=b572)
6. [Zerojudge d490: 我也愛偶數](https://zerojudge.tw/ShowProblem?problemid=d490)
7. [Zerojudge d039: 11044 - Searching for Nessy](https://zerojudge.tw/ShowProblem?problemid=d039)
:::spoiler <font color="FEA0A0">**題解**</font>
1. [Zerojudge a004: 文文的求婚](https://zerojudge.tw/ShowProblem?problemid=a004)
```cpp=
#include<iostream>
using namespace std;
int main(){
int y;
while(cin >> y){
if(y%4 == 0){
if(y%100 == 0 && y%400 != 0){
cout << "平年" << endl;
}
else{
cout << "閏年" << endl;
}
}
else{
cout << "平年" << endl;
}
}
return 0;
}
```
##
2. [Zerojudge a024: 最大公因數(GCD)](https://zerojudge.tw/ShowProblem?problemid=a024)
使用輾轉相除法
```cpp=
#include<iostream>
using namespace std;
int main(){
int a, b;
cin >> a >> b;
while(b > 0){
a %= b;
swap(a, b);
}
cout << a;
return 0;
}
```
##
3. [Zerojudge a010: 因數分解](https://zerojudge.tw/ShowProblem?problemid=a010)
```cpp=
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n, times;
cin >> n;
for(int i=2; i<=n; i++){
times = 0; //用來計算除的次數
while(n%i == 0){
n /= i;
times++;
}
if(times > 1){
cout << i << '^' << times;
if(n != 1){
cout << " * ";
}
}
else if(times == 1){
cout << i;
if(n != 1 ){
cout << " * ";
}
}
}
return 0;
}
```
##
4. [Zerojudge a005: Eva 的回家作業](https://zerojudge.tw/ShowProblem?problemid=a005)
```cpp=
#include<iostream>
using namespace std;
int main(){
int n;
int a, b, c, d;
cin >> n;
for(int i=0; i<n; i++){
cin >> a >> b >> c >> d;
cout << a << " " << b << " " << c << " " << d << " ";
if( b-a == c-b && c-b == d-c ){
cout << (d-c) + d << "\n";
}
else if( b/a == c/b && c/b == d/c ){
cout << (d/c) * d << "\n";
}
}
return 0;
}
```
##
5. [Zerojudge b572: 忘了東西的傑克](https://zerojudge.tw/ShowProblem?problemid=b572)
```cpp=
#include<iostream>
using namespace std;
int main(){
int n, h1, h2, m1, m2, ans, t;
cin >> n;
for(int i=0; i<n; i++){
cin >> h1 >> m1 >> h2 >> m2 >> ans;
if(m2 < m1){
m2 += 60; //借位
h2--;
}
t = 60 * (h2-h1) + (m2-m1);
if(t >= ans) cout << "Yes\n";
else cout << "No\n";
}
return 0;
}
```
<!--
6. [Zerojudge d490: 我也愛偶數](https://zerojudge.tw/ShowProblem?problemid=d490)
7. [Zerojudge d039: 11044 - Searching for Nessy](https://zerojudge.tw/ShowProblem?problemid=d039)
-->
(待補)
:::
## for 迴圈
<font color="FEA0A0">**題目**</font>
1. [Zerojudge a244: 新手訓練 ~ for + if](https://zerojudge.tw/ShowProblem?problemid=a244)
2. [Zerojudge d074: 電腦教室](https://zerojudge.tw/ShowProblem?problemid=d074)
3. [Zerojudge d498: 我不說髒話](https://zerojudge.tw/ShowProblem?problemid=d498)
4. [Zerojudge a148: You Cannot Pass?!](https://zerojudge.tw/ShowProblem?problemid=a148)
5. [Kattis FizzBuzz](https://open.kattis.com/problems/fizzbuzz)
6. [TOJ 577 / 力量](https://toj.tfcis.org/oj/pro/577/)
:::spoiler <font color="FEA0A0">**題解**</font>
<!--
1. [Zerojudge a244: 新手訓練 ~ for + if](https://zerojudge.tw/ShowProblem?problemid=a244)
2. [Zerojudge d074: 電腦教室](https://zerojudge.tw/ShowProblem?problemid=d074)
3. [Zerojudge d498: 我不說髒話](https://zerojudge.tw/ShowProblem?problemid=d498)
4. [Zerojudge a148: You Cannot Pass?!](https://zerojudge.tw/ShowProblem?problemid=a148)
5. [Kattis FizzBuzz](https://open.kattis.com/problems/fizzbuzz)
6. [TOJ 577 / 力量](https://toj.tfcis.org/oj/pro/577/)
-->
(待補)
:::
## 巢狀迴圈
<font color="FEA0A0">**題目**</font>
1. 反覆輸出高是 n 的正三角形

2. [Zerojudge d649: 數字三角形](https://zerojudge.tw/ShowProblem?problemid=d649)
3. [Zerojudge c013: 00488 - Triangle Wave](https://zerojudge.tw/ShowProblem?problemid=c013)
4. [TOJ 104 / 星星樹](https://toj.tfcis.org/oj/pro/104/)
5. [TOJ 110 / 六芒星的咒符](https://toj.tfcis.org/oj/pro/110/)
:::spoiler <font color="FEA0A0">**題解**</font>
1. 反覆輸出高是 n 的正三角形
```cpp=
#include <iostream>
using namespace std;
int main() {
int n;
while(cin >> n){
for(int i=1; i<=n; i++){
for(int space=0; space<n-i ; space++) cout << " ";
for(int stars=0; stars<i*2-1; stars++) cout << "*";
cout << "\n";
}
}
return 0;
}
```
##
2. [Zerojudge d649: 數字三角形](https://zerojudge.tw/ShowProblem?problemid=d649)
```cpp=
#include<iostream>
using namespace std;
int main(){
int n;
while(cin>>n && n){
for(int i=1; i<=n; i++){
for(int j=0; j<n-i; j++) cout << '_';
for(int j=0; j<i ; j++) cout << '+';
cout<<'\n';
}
}
return 0;
}
```
##
3. [Zerojudge c013: 00488 - Triangle Wave](https://zerojudge.tw/ShowProblem?problemid=c013)
```cpp=
#include <iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){ //輸出多少組 waves
int h,n;
cin>>h>>n;
while(n--){ //一組 wave 輸出多少個
for(int i=1;i<=h;i++){ //上半
for(int j=0;j<i;j++){ //輸出數字
cout<<i;
}
cout<<'\n';
}
for(int i=1;i<h;i++){ //下半
for(int j=h;j>i;j--){ //輸出數字
cout<<h-i;
}
cout<<'\n';
}
cout<<'\n';
}
}
return 0;
}
```
<!--
4. [TOJ 104 / 星星樹](https://toj.tfcis.org/oj/pro/104/)
5. [TOJ 110 / 六芒星的咒符](https://toj.tfcis.org/oj/pro/110/)
-->
(待補)
:::
## switch
<font color="FEA0A0">**題目**</font>
1. [Zerojudge a053: Sagit's 計分程式](https://zerojudge.tw/ShowProblem?problemid=a053)
2. [Zerojudge a244: 新手訓練 ~ for + if](https://zerojudge.tw/ShowProblem?problemid=a244) (請試著用switch解)
:::spoiler <font color="FEA0A0">**題解**</font>
<!--
1. [Zerojudge a053: Sagit's 計分程式](https://zerojudge.tw/ShowProblem?problemid=a053)
2. [Zerojudge a244: 新手訓練 ~ for + if](https://zerojudge.tw/ShowProblem?problemid=a244) (請試著用switch解)
-->
(待補)
:::