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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 計算總數小于給定值的三元組 > 原文: [https://www.geeksforgeeks.org/count-triplets-with-sum-smaller-that-a-given-value/](https://www.geeksforgeeks.org/count-triplets-with-sum-smaller-that-a-given-value/) 給定一個由不同整數組成的數組和一個求和值。 查找總和小于給定總和值的三元組的數量。 預期時間復雜度為 `O(n^2)`。 **示例**: ``` Input : arr[] = {-2, 0, 1, 3} sum = 2. Output : 2 Explanation : Below are triplets with sum less than 2 (-2, 0, 1) and (-2, 0, 3) Input : arr[] = {5, 1, 3, 4, 7} sum = 12. Output : 4 Explanation : Below are triplets with sum less than 12 (1, 3, 4), (1, 3, 5), (1, 3, 7) and (1, 4, 5) ``` 一個**簡單解決方案**是運行三個循環,一個接一個地考慮所有三元組。 對于每個三元組,如果三元組的總和小于給定的總和,則比較總和和增量計數。 ## C++ ```cpp // A Simple C++ program to count triplets with sum smaller // than a given value #include<bits/stdc++.h> using namespace std; int countTriplets(int arr[], int n, int sum) { ????// Initialize result ????int ans = 0; ????// Fix the first element as A[i] ????for (int i = 0; i < n-2; i++) ????{ ???????// Fix the second element as A[j] ???????for (int j = i+1; j < n-1; j++) ???????{ ???????????// Now look for the third number ???????????for (int k = j+1; k < n; k++) ???????????????if (arr[i] + arr[j] + arr[k] < sum) ???????????????????ans++; ???????} ????} ????return ans; } // Driver program int main() { ????int arr[] = {5, 1, 3, 4, 7}; ????int n = sizeof arr / sizeof arr[0]; ????int sum = 12; ????cout << countTriplets(arr, n, sum) << 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>

                              哎呀哎呀视频在线观看