<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/replace-two-consecutive-equal-values-one-greater/](https://www.geeksforgeeks.org/replace-two-consecutive-equal-values-one-greater/) 系統會為您提供大小為`n`的數組。 您必須每次用單個值`x + 1`替換每對連續的值`x`,直到不再有這樣的重復,然后再打印新數組。 > 輸入:5、2、1、1、2、2 > 輸出:5 4 > **說明**: > 步驟 1:遍歷時遇到一對 1(取為 2)。 我們得到 5、2、2、2、2 > 步驟 2:將第一個遇到的 2 對替換為 3。我們得到 5、3、2、2 > 步驟 3:再次將 2 對替換為 3,我們得到 5、3、3 > 步驟 4:將最近形成的 3 對換成 4。我們得到 5、4 > 這是我們所需要的答案。 > > 輸入:4,5,11,2,5,7,2 > 輸出:4 5 11 2 5 7 2 **方法**:在此問題中,您必須遍歷整數數組并檢查是否有兩個連續的整數具有相同的值`X`。然后必須用單個整數`X + 1`替換該對整數。 之后,您必須通過重新遍歷數組并執行相同的操作來開始新的步驟。 ## C++ ```cpp // C++ program to replace two elements with equal // values with one greater. #include <bits/stdc++.h> using namespace std; // Function to replace consecutive equal? // elements void replace_elements(int arr[], int n) { ????int pos = 0;? // Index in result ????for (int i = 0; i < n; i++) { ????????arr[pos++] = arr[i]; ????????while (pos > 1 && arr[pos - 2] ==? ??????????????????????????arr[pos - 1]) { ????????????pos--; ????????????arr[pos - 1]++; ????????} ????} ????// to print new array ????for (int i = 0; i < pos; i++) ????????cout << arr[i] << " "; } // Driver Code int main() { ????int arr[] = { 6, 4, 3, 4, 3, 3, 5 }; ????int n = sizeof(arr) / sizeof(int); ????replace_elements(arr, n); ????return 0; } ``` ## Java ```java // java program to replace two elements // with equal values with one greater. public class GFG { ????// Function to replace consecutive equal? ????// elements ????static void replace_elements(int arr[], int n) ????{ ????????int pos = 0; // Index in result ????????for (int i = 0; i < n; i++) { ????????????arr[pos++] = arr[i]; ????????????while (pos > 1 && arr[pos - 2] ==? ??????????????????????????????????arr[pos - 1]) ????????????{ ????????????????pos--; ????????????????arr[pos - 1]++; ????????????} ????????} ????????// to print new array ????????for (int i = 0; i < pos; i++) ????????????System.out.print( arr[i] + " "); ????} ????// Driver code ????public static void main(String args[]) ????{ ????????int arr[] = { 6, 4, 3, 4, 3, 3, 5 }; ????????int n = arr.length; ????????replace_elements(arr, n); ????} } // This code is contributed by Sam007 ``` ## Python3 ```py # python program to replace two elements # with equal values with one greater. from __future__ import print_function # Function to replace consecutive equal? # elements def replace_elements(arr, n): ????pos = 0 # Index in result ????for i in range(0, n): ????????arr[pos] = arr[i] ????????pos = pos + 1 ????????while (pos > 1 and arr[pos - 2] ????????????????????????== arr[pos - 1]): ????????????pos -= 1 ????????????arr[pos - 1] += 1 ????# to print new array ????for i in range(0, pos): ????????print(arr[i], end=" ") # Driver Code arr = [ 6, 4, 3, 4, 3, 3, 5 ] n = len(arr) replace_elements(arr, n) # This code is contributed by Sam007 ``` ## C# ```cs // C# program to replace two elements // with equal values with one greater. using System; class GFG { ????// Function to replace consecutive equal? ????// elements ????static void replace_elements(int []arr, int n) ????{ ????????int pos = 0; // Index in result ????????for (int i = 0; i < n; i++) { ????????????arr[pos++] = arr[i]; ????????????while (pos > 1 && arr[pos - 2] ==? ??????????????????????????????????arr[pos - 1]) ????????????{ ????????????????pos--; ????????????????arr[pos - 1]++; ????????????} ????????} ????????// to print new array ????????for (int i = 0; i < pos; i++) ????????????Console.Write( arr[i] + " "); ????} ????// Driver code ????static void Main() ????{ ????????int []arr = { 6, 4, 3, 4, 3, 3, 5 }; ????????int n = arr.Length; ????????replace_elements(arr, n); ????} } // This code is contributed by Sam007 ``` ## PHP ```php <?php // PHP program to replace two? // elements with equal // values with one greater. // Function to replace consecutive // equal elements function replace_elements($arr,$n) { ????// Index in result ????$pos = 0;? ????for ($i = 0; $i < $n; $i++)? ????{ ????????$arr[$pos++] = $arr[$i]; ????????while ($pos > 1 && $arr[$pos - 2] ==? ????????????????????????$arr[$pos - 1]) ????????{ ????????????$pos--; ????????????$arr[$pos - 1]++; ????????} ????} ????// to print new array ????for ($i = 0; $i < $pos; $i++) ????????echo $arr[$i] . " "; } ????// Driver Code ????$arr = array(6, 4, 3, 4, 3, 3, 5); ????$n = count($arr); ????replace_elements($arr, $n); // This code is contributed by Sam007\. ?> ``` **輸出**: ``` 6 4 3 6 ``` * * * * * *
                  <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>

                              哎呀哎呀视频在线观看