<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++ 程序:查找 GCD > 原文: [https://www.programiz.com/cpp-programming/examples/hcf-gcd](https://www.programiz.com/cpp-programming/examples/hcf-gcd) #### 有關使用循環和決策語句來計算兩個整數(正整數和負整數)的 GCD 的不同方法的示例。 要理解此示例,您應該了解以下 [C++ 編程](/cpp-programming "C++ tutorial")主題: * [C++ `if`,`if...else`和嵌套`if...else`](/cpp-programming/if-else) * [C++ `for`循環](/cpp-programming/for-loop) * [C++ `while`和`do...while`循環](/cpp-programming/do-while-loop) * * * 可以完美除以兩個整數的最大整數稱為這兩個數字的 GCD 或 HCF。 * * * ### 示例 1:使用`while`循環查找 GCD ```cpp #include <iostream> using namespace std; int main() { int n1, n2; cout << "Enter two numbers: "; cin >> n1 >> n2; while(n1 != n2) { if(n1 > n2) n1 -= n2; else n2 -= n1; } cout << "HCF = " << n1; return 0; } ``` **輸出** ```cpp Enter two numbers: 78 52 HCF = 26 ``` 在上面的程序中,從較大的數字中減去較小的數字,并存儲該數字以代替較大的數字。 繼續此過程,直到兩個數字相等,即 HCF。 * * * ### 示例:2.使用`for`循環查找 HCF/GCD ```cpp #include <iostream> using namespace std; int main() { int n1, n2, hcf; cout << "Enter two numbers: "; cin >> n1 >> n2; // Swapping variables n1 and n2 if n2 is greater than n1. if ( n2 > n1) { int temp = n2; n2 = n1; n1 = temp; } for (int i = 1; i <= n2; ++i) { if (n1 % i == 0 && n2 % i ==0) { hcf = i; } } cout << "HCF = " << hcf; return 0; } ``` 該程序的邏輯很簡單。 在該程序中,`n1`和`n2`之間的小整數存儲在`n2`中。 然后,循環從`i = 1`迭代到`i <= n2`,并且在每次迭代中,`i`的值增加 1。 如果兩個數字都可以被`i`整除,則該數字將存儲在變量`hcf`中。 迭代完成后,HCF 將存儲在變量`hcf`中。
                  <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>

                              哎呀哎呀视频在线观看