首頁 > 後端開發 > C++ > 主體

使用C++編寫的數組元素排序的排名

王林
發布: 2023-08-26 22:45:12
轉載
1258 人瀏覽過

使用C++編寫的數組元素排序的排名

在給定的問題中,我們需要對陣列的所有給定元素進行排名,最小的數字具有最小的排名,最大的具有最大的排名。例如,我們還需要根據數字的頻率來更改數字的排名-

Input : 20 30 10
Output : 2.0 3.0 1.0

Input : 10 12 15 12 10 25 12
Output : 1.5, 4.0, 6.0, 4.0, 1.5, 7.0, 4.0

Here the rank of 10 is 1.5 because there are two 10s present in the given array now if we assume they both take different ranks i.e. 1 and 2 and we thus divide it within themselves so their rank becomes 1.5 and 1.5.

Input : 1, 2, 5, 2, 1, 60, 3
Output : 1.5, 3.5, 6.0, 3.5, 1.5, 7.0, 5.0
登入後複製

尋找解決方案的方法

有兩種不同的方法來尋找解決方案,它們是-

暴力方法

在這種方法中,我們將循環,選擇任何特定元素,並確定其排名。

範例

#include <bits/stdc++.h>
using namespace std;

int main() {
   int arr[] = {1, 2, 5, 2, 1, 25, 2}; // given array
   int n = sizeof(arr) / sizeof(arr[0]); // size of our given array

   float rank[n] = {0}; // our ranking array
   for (int i = 0; i < n; i++) {
      int r = 1; // the number of elements greater than arr[i]
      int s = 1; // the number of elements equal to arr[i]

      for (int j = 0; j < n; j++) {
         if (j != i && arr[j] < arr[i])
            r += 1;
   
         if (j != i && arr[j] == arr[i])
            s += 1;
      }
      rank[i] = r + (float)(s - 1) / (float) 2; // using formula
      //to obtain rank of particular element

   }

   for (int i = 0; i < n; i++) // outputting the ranks
      cout << rank[i] << &#39; &#39;;

   return 0;
}
登入後複製

輸出

1.5 4 6 4 1.5 7 4
登入後複製

該程式的時間複雜度為O(N*N),其中N是現在給定陣列的大小;正如你所看到的,我們的時間複雜度不好,因此我們將提高其效率,以更好地適應更高的限制。

高效方法

在這種方法中,我們將採取一個新數組並對其進行排序,因為數組已排序,現在我們知道相同排名的所有元素將在一起,因此現在我們像往常一樣對它們進行排名,然後計算特定元素的排名。

範例

#include <bits/stdc++.h>

using namespace std;

int main() {
   int arr[] = {1, 2, 5, 2, 1, 60, 3}; // given array
   int n = sizeof(arr) / sizeof(arr[0]); // size of our given array
   float rank[n] = {0}; // our ranking array
   int old[n];
   for(int i = 0; i < n; i++)
   old[i] = arr[i];
   sort(arr, arr+n); // sorting the array
   int prev = arr[0];
   int r = 1; // ranks
   int s = 0; // frequency
   int tot = 0; // will stack up all the rank contained by an element
   map<int, float> rrank;

   for (int i = 0; i < n; i++) {
      if(prev == arr[i]) {
         s++;
         tot += r;
      } else {
         float now = 0;
         now = (float)tot/s; // dividing the ranks equally
         rrank[prev] = now;
         prev = arr[i];
         tot = r;
         s = 1;
      }
      r++;
   }
   rrank[arr[n-1]] = (float)tot/s;
   for (int i = 0; i < n; i++) // outputting the ranks
      cout << rrank[old[i]] << " ";

   return 0;
}
登入後複製

輸出

1.5 3.5 6 3.5 1.5 7 5
登入後複製

上述程式碼的說明

在這個方法中,我們對陣列進行排序,然後從頭開始對每個元素進行排名(排名從1 開始)。現在,如果我們的上一個元素等於當前元素,我們就會增加 s 並疊加到我們的排名總和。當我們的元素發生更改時,我們將前面的元素的排名分開,刷新 s 和總計,然後繼續我們的程式碼。

結論

在本文中,我們解決了一個問題來找到陣列中所有元素的排名。我們也學習了解決這個問題的C 程序以及解決這個問題的完整方法(正常且有效率)。我們可以用其他語言寫相同的程序,例如C、java、python等語言。

以上是使用C++編寫的數組元素排序的排名的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!