⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rank.cpp

📁 datastucutre and algorithms, application, in C
💻 CPP
字号:
// compute element ranks

#include <iostream>
#include <algorithm> // has copy

using namespace std;


template<class T>
void rank(T a[], int n, int r[])
{// Rank the n elements a[0:n-1].
 // Element ranks returned in r[0:n-1]
   for (int i = 0; i < n; i++)
      r[i] = 0;  // initialize

   // compare all element pairs
   for (int i = 1; i < n; i++)
      for (int j = 0; j < i; j++)
         if (a[j] <= a[i]) r[i]++;
         else r[j]++;
}

int main()
{
   int a[6] = {2, 6, 4, 3, 1, 5};
   int r[6];

   // output the elements
   cout << "a[0:5] = ";
   copy(a, a+6, ostream_iterator<int>(cout, " "));
   cout << endl;

   // determine the ranks
   rank(a,6,r);

   // output the ranks
   cout << "r[0:5] = ";
   copy(r, r+6, ostream_iterator<int>(cout, " "));
   cout << endl;
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -