<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C++ 程序:查找 LCM > 原文: [https://www.programiz.com/cpp-programming/examples/lcm](https://www.programiz.com/cpp-programming/examples/lcm) #### 使用循環和決策語句來計算兩個整數的 LCM(最小公倍數)的不同方法的示例。 要理解此示例,您應該了解以下 [C++ 編程](/cpp-programming "C++ tutorial")主題: * [C++ `if`,`if...else`和嵌套`if...else`](/cpp-programming/if-else) * [C++ `while`和`do...while`循環](/cpp-programming/do-while-loop) * * * 兩個整數`a`和`b`的 LCM 是可被`a`和`b`整除的最小正整數。 * * * ### 示例 1:查找 LCM ```cpp #include <iostream> using namespace std; int main() { int n1, n2, max; cout << "Enter two numbers: "; cin >> n1 >> n2; // maximum value between n1 and n2 is stored in max max = (n1 > n2) ? n1 : n2; do { if (max % n1 == 0 && max % n2 == 0) { cout << "LCM = " << max; break; } else ++max; } while (true); return 0; } ``` **輸出** ```cpp Enter two numbers: 12 18 LCM = 36 ``` 在上述程序中,要求用戶對兩個整數`n1`和`n2`進行整數運算,并將這兩個數字中的最大值存儲在`max`中。 檢查`max`是否可被`n1`和`n2`整除,如果兩個數均可以整除`max`(包含 LCM) 打印并終止循環。 如果不是,則將`max`的值加 1,然后繼續相同的過程,直到`max`被`n1`和`n2`整除。 * * * ### 示例 2:使用 HCF 查找 LCM 兩個數字的 LCM 由下式給出: ```cpp LCM = (n1 * n2) / HCF ``` 訪問此頁面以了解:[如何在 C++ 中計算 HCF?](/cpp-programming/examples/hcf-gcd "C++ HCF") ```cpp #include <iostream> using namespace std; int main() { int n1, n2, hcf, temp, lcm; cout << "Enter two numbers: "; cin >> n1 >> n2; hcf = n1; temp = n2; while(hcf != temp) { if(hcf > temp) hcf -= temp; else temp -= hcf; } lcm = (n1 * n2) / hcf; cout << "LCM = " << lcm; return 0; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看