fork download
  1. #include <bits/stdc++.h>
  2. #include<stdio.h>
  3. #include<memory.h>
  4. #include<limits.h>
  5. #include<string.h>
  6. using namespace std;
  7.  
  8. struct Node {
  9. int i,j;
  10. float val;
  11. Node(int ii,int jj,float v) : i(ii), j(jj), val(v) {};
  12. };
  13.  
  14. bool cmp(Node &a,Node &b) {
  15. if (a.val < b.val) return true;
  16. return false;
  17. }
  18.  
  19. vector<int> solve(vector<int>&nums,int k) {
  20. vector<int> ans;
  21. int n=nums.size();
  22. vector<Node> temp;
  23.  
  24.  
  25. for(int i=0;i<n;i++) {
  26. for(int j=i+1;j<n;j++) {
  27. temp.push_back(Node(nums[i],nums[j],1.0*nums[i]/nums[j]));
  28. }
  29. }
  30.  
  31.  
  32. sort(temp.begin(),temp.end(),cmp);
  33. ans.push_back(temp[k-1].i);
  34. ans.push_back(temp[k-1].j);
  35. return ans;
  36. }
  37.  
  38. int main() {
  39. vector<int> nums = {1,2,3,5};
  40. int k=3;
  41. auto ans = solve(nums,3);
  42. cout<<ans[0]<<" "<<ans[1]<<endl;
  43. }
Success #stdin #stdout 0s 5280KB
stdin
geeksforgeeks
geeksforgeeks
geeksforgeeks
stdout
2 5