<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 計算將`L-R`范圍內的所有數字相除的元素 > 原文: [https://www.geeksforgeeks.org/count-elements-which-divide-all-numbers-in-range-l-r/](https://www.geeksforgeeks.org/count-elements-which-divide-all-numbers-in-range-l-r/) 給定`N`個數字和`Q`個查詢,每個查詢都由`L`和`R`組成。任務是編寫一個打印數字計數的程序,該程序將給定范圍`L-R`中的所有數字相除。 **示例**: ``` Input : a = {3, 4, 2, 2, 4, 6} Q = 2 L = 1 R = 4 L = 2 R = 6 Output : 0 2 Explanation : The range 1-4 has {3, 4, 2, 2} which does not have any number that divides all the numbers in this range. The range 2-6 has {4, 2, 2, 4, 6} which has 2 numbers {2, 2} which divides all numbers in the given range. Input: a = {1, 2, 3, 5} Q = 2 L = 1 R = 4 L = 2 R = 4 Output: 1 0 ``` **樸素的方法**:針對每個查詢從范圍`L-R`進行迭代,并檢查索引`i`處的給定元素是否將范圍中的所有數字相除。 我們對所有元素進行計數,將所有數字相除。 在最壞情況下,每個查詢的復雜度將為`O(n^2)`。 **以下是樸素方法的實現**: ## C++ ```cpp // CPP program to Count elements which // divides all numbers in range L-R #include <bits/stdc++.h> using namespace std; // function to count element // Time complexity O(n^2) worst case int answerQuery(int a[], int n,? ????????????????int l, int r) { ????// answer for query ????int count = 0; ????// 0 based index ????l = l - 1; ????// iterate for all elements ????for (int i = l; i < r; i++)? ????{ ????????int element = a[i]; ????????int divisors = 0; ????????// check if the element divides ????????// all numbers in range ????????for (int j = l; j < r; j++)? ????????{ ????????????// no of elements ????????????if (a[j] % a[i] == 0) ????????????????divisors++; ????????????else ????????????????break; ????????} ????????// if all elements are divisible by a[i] ????????if (divisors == (r - l)) ????????????count++; ????} ????// answer for every query ????return count; } // Driver Code int main() { ????int a[] = { 1, 2, 3, 5 }; ????int n = sizeof(a) / sizeof(a[0]); ????int l = 1, r = 4; ????cout << answerQuery(a, n, l, r) << endl; ????l = 2, r = 4;???? ????cout << answerQuery(a, n, l, r) << endl; ????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>

                              哎呀哎呀视频在线观看