We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>#include<iomanip>usingnamespacestd;intgetIndexOf(doublea,vector<double>y){intcount=1;for(intj=0;j<y.size();++j){if(a==y[j])break;elsecount++;}returncount;}vector<int>getRank(vector<double>x){vector<int>xVar(x.size());vector<double>x_sorted(x.size());partial_sort_copy(begin(x),end(x),begin(x_sorted),end(x_sorted));for(inti=0;i<x.size();++i){intindex=getIndexOf(x[i],x_sorted);xVar[i]=index;}returnxVar;}intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intn;cin>>n;vector<double>x,y;doublenum=0;for(inti=0;i<n;++i){cin>>num;x.push_back(num);}for(inti=0;i<n;++i){cin>>num;y.push_back(num);}vector<int>rankX=getRank(x);vector<int>rankY=getRank(y);doubler=0;for(inti=0;i<n;++i){r+=pow((rankX[i]-rankY[i]),2.);}cout<<fixed<<setprecision(3);cout<<1-6*r/(n*n*n-n);return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 7: Spearman's Rank Correlation Coefficient
You are viewing a single comment's thread. Return to all comments →
C++