---
# System prepended metadata

title: 108題目(B卷)
tags: [C++複習講義]

---

---
tags: C++複習講義
---

# 108題目(B卷)

[回目錄](/jHU9MQS5QS-Uvs-ZS6CSxg)

:::success
為了幫(強)助(迫)各位思考，以下僅提供<font color=#f00>部分程式碼</font>，有任何問題歡迎私訊[資訊社IG](https://www.instagram.com/dzif_22nd_/?hl=zh-tw)。
程式碼內的 `__?__` 是刻意挖掉的，希望同學能先自行思考後將其還原
:::

## 最大公因數及最小公倍數
> 輸入三正整數，求其最大公因數及最小公倍數

```cpp=
    while (__?__)
    {
        double a1, b1, c1; //double 浮點數
        int a, b, c, min, max;
        cout << "請輸入A";
        cin >> a1;
        cout << "請輸入B";
        cin >> b1;
        cout << "請輸入C";
        cin >> c1;
        if ((double)(int)a1 != a1 || (double)(int)b1 != b1 || (double)(int)c1 != c1 || a1 <= 0 || b1 <= 0 || c1 <= 0)
        {
            cout << "請確認你輸入的數字！";
            continue;
        }
        a = (int)a1; //之後的%不接受浮點數
        b = (int)b1;
        c = (int)c1;
        if (a >= b && a >= c)
        {
            max = a; //找max-最小公倍數大於三數中最大數
        }
        else if (b >= a && b >= c)
        {
            max = b;
        }
        else
        {
            max = c;
        }
        if (a <= b && a <= c)
        {
            min = a; //找min-最大公因數小於三數中最小數
        }
        else if (b <= a && b <= c)
        {
            min = b;
        }
        else
        {
            min = c;
        }
        int x = 1;
        for (int i = 2; i <= __?__; i++)
        {
            if (a % i == 0 && b % i == 0 && c % i == 0)
            {
                x = __?__;
            }
        }
        int y = max;
        while (y % a != 0 || y % b != 0 || y % c != 0)
        {
            y += 1;
        }
        cout << a << "、" << b << "、" << c << "之最大公因數為" << x << "最小公倍數為" << y << endl;
    }
```
::: spoiler 參考答案
```cpp=
    while (true)
    {
        double a1, b1, c1;
        int a, b, c, min, max;
        cout << "請輸入A";
        cin >> a1;
        cout << "請輸入B";
        cin >> b1;
        cout << "請輸入C";
        cin >> c1;
        if ((double)(int)a1 != a1 || (double)(int)b1 != b1 || (double)(int)c1 != c1 || a1 <= 0 || b1 <= 0 || c1 <= 0)
        {
            cout << "請確認你輸入的數字！";
            continue;
        }
        a = (int)a1;
        b = (int)b1;
        c = (int)c1;
        if (a >= b && a >= c)//找出最大
        {
            max = a;
        }
        else if (b >= a && b >= c)
        {
            max = b;
        }
        else
        {
            max = c;
        }
        if (a <= b && a <= c)//找出最小
        {
            min = a;
        }
        else if (b <= a && b <= c)
        {
            min = b;
        }
        else
        {
            min = c;
        }
        int x = 1;
        for (int i = 2; i <= min; i++) //找最大公因數
        {
            if (a % i == 0 && b % i == 0 && c % i == 0)
            {
                x = i;
            }
        }
        int y = max;
        while (y % a != 0 || y % b != 0 || y % c != 0) //找最小公倍數
        {
            y += 1;
        }
        cout << a << "、" << b << "、" << c << "之最大公因數為" << x << "最小公倍數為" << y << endl;
    }
```
:::

## 兩正整數間質數，並求其總和
> 輸入兩正整數，列出兩數之間所有質數並加總
> 質數 : 因數只有1和自己
```cpp=
    while (true)
    {
        double n1, m1;
        cout << "請輸入整數n";
        cin >> n1;
        cout << "請輸入整數m";
        cin >> m1;
        if ((double)(int)n1 != n1 || (double)(int)m1 != m1 || n1 <= 0 || n1 <= 0)//確認是否輸入整數
        {
            cout << "數字輸入錯誤！" << endl;
            continue;
        }
        int n = n1, m = m1, sum = 0;
        if (__?__)
        {
            int temp; //臨時變數
            temp = n;
            n = m;
            m = temp;
        }
        cout << n << "~" << m << "之間的質數有：";
        for (int i = n; i <= m; i++)
        {
            bool is = true;//假設其為質數
            for (int k = 2; k < i; k++)
            {
                if (i % k == 0)
                {
                    is = false;//確認不是質數
                    break;
                }
            }
            if (is)//如果是質數
            {
                cout << i << "\t";
                sum += i;
            }
        }
        cout << "\n"
             << "其總和為：" << sum << endl
             << endl;
    }
```
::: spoiler 參考答案
```cpp=
    while (true)
    {
        double n1, m1;
        cout << "請輸入整數n";
        cin >> n1;
        cout << "請輸入整數m";
        cin >> m1;
        if ((double)(int)n1 != n1 || (double)(int)m1 != m1 || n1 <= 0 || n1 <= 0)//確認是否輸入整數
        {
            cout << "數字輸入錯誤！" << endl;
            continue;
        }
        int n = n1, m = m1, sum = 0;
        if (n > m)
        {
            int temp;
            temp = n;
            n = m;
            m = temp;
        }
        cout << n << "~" << m << "之間的質數有：";
        for (int i = n; i <= m; i++)
        {
            bool is = true;
            for (int k = 2; k < i; k++)
            {
                if (i % k == 0)
                {
                    is = false;//確認不是質數
                    break;
                }
            }
            if (is)//如果是質數
            {
                cout << i << "\t";
                sum += i;
            }
        }
        cout << "\n"
             << "其總和為：" << sum << endl
             << endl;
    }
```
:::
## 隨機亂數兩十位正整數N跟M，並計算總合
> 產生兩個十位數亂數，輸入其總和，並判斷加總正確或者錯誤
> 取亂數公式 : 大數減小數再加1 
```cpp=
#include <ctime>
```
```cpp=
    while (true)
    {
        srand(time(NULL)) //下亂數種子
        int n = rand() % (__?__) + 9;
        int m = rand() % (__?__) + 9;
        int sum = n + m;
        int input;
        cout << "請計算" << n << "+" << m << "=";
        cin >> input;
        if (input == sum)
        {
            cout << "你答對了，答案就是" << sum;
        }
        else
        {
            cout << "你答錯了，答案是" << sum;
        }
        cout << endl
             << endl;
    }
```
::: spoiler 參考答案

```cpp=
#include <ctime>
```
```cpp=
    while (true)
    {
        srand(time(NULL));//下亂數種子
        int n = rand() % (99 - 9 + 1) + 9;
        int m = rand() % (99 - 9 + 1) + 9;
        int sum = n + m;
        int input;
        cout << "請計算" << n << "+" << m << "=";
        cin >> input;
        if (input == sum)
        {
            cout << "你答對了，答案就是" << sum;
        }
        else
        {
            cout << "你答錯了，答案是" << sum;
        }
        cout << endl
             << endl;
    }
::: 
## 級數
> 輸入一數，若為偶數，則計算1;若為奇數，則計算2
1.$\sqrt{2}+\sqrt{4}+\sqrt{6}+···+\sqrt{n}$
2.$1/1+1/3+1/5+···+1/n$
```cpp=
#include <cmath>
```
```cpp=
    while (true)
    {
        double nn;
        double output = 0;
        cout << "請輸入整數N：";
        cin >> nn;
        if ((double)(int)nn != nn || nn < 0)//確認是否輸入整數
        {
            cout << "請輸入整數！" << endl;
            continue;
        }
        int n = (int)nn;
        if (n % 2 == 0)
        {
            for (int i = 2; i <= n; i += 2)
            {
                output+=sqrt(i);
            }
        }
        else
        {
            for (int i = 1; i <= n; i += 2)
            {
                output += 1.0 / i;
            }
        }
        cout << n << "的級數：" << output << endl;
    }
```
::: spoiler 參考答案
```cpp=
#include <cmath>
```
```cpp=
    while (true)
    {
        double nn;
        double output = 0;
        cout << "請輸入整數N：";
        cin >> nn;
        if ((double)(int)nn != nn || nn < 0)//確認是否輸入整數
        {
            cout << "請輸入整數！" << endl;
            continue;
        }
        int n = (int)nn;
        if (n % 2 == 0)
        {
            for (int i = 2; i <= n; i += 2)
            {
                output += sqrt(i);
            }
        }
        else
        {
            for (int i = 1; i <= n; i += 2)
            {
                output += 1.0 / i;
            }
        }
        cout << n << "的級數：" << output << endl;
    }
```
::: 