-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7_2
42 lines (40 loc) · 1.05 KB
/
day7_2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//In this challenge, we practice calculating Spearman's rank correlation coefficient.//
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
// Credit to mohammedabdulba1 on hackerrank.com discussion forums for the solution.
void get_data(vector<double> &data)
{
for(auto &item:data)
cin>>item;
}
int find_rank(vector<double> v, int i)
{
vector<double> vsort;
vsort = v;
sort(vsort.begin(),vsort.end());
vector<double>::iterator it;
it = find(vsort.begin(),vsort.end(),v[i]);
return distance(vsort.begin(), it) +1;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
double rankdiff = 0.0,prcc = 0.0;
int n;
cin>>n;
vector<double> x(n);
vector<double> y(n);
get_data(x);
get_data(y);
for(int i =0 ; i<n; i++)
{
rankdiff +=pow((find_rank(x,i)-find_rank(y, i)),2);
}
prcc = 1- (6*rankdiff)/(n*(pow(n,2)-1));
cout<<fixed<<setprecision(3)<<prcc;
return 0;
}