Menu Close

Simon has given N ratios in the form of A and B that is represented as A/B.

Simon has given N ratios in the form of A and B that is represented as A/B. The values of A and B are represented as double data type values. The values of B are incorrect. The actual values of B are B+R. Simon know the actual sum of all the ratios that is available in variable K.

Note: The true values of B, represented as (B+R), are always greater than 0. Simon’s task is to determine the value of R.

Constraints:
1 <= N <= 1000
1 <= A <= 1000
|B| <= 1000
1 <= K <= 10^6

Input Format:
First line: Two integers N and col denoting the number of ratios and the value 2 respectively
Next N lines: Each line contains two double values A and B
Last line: A double value K denoting the sum of all the ratios

Output Format:
Print the value of R. Simon’s answer must contain an absolute or relative error of less than 10^-6.

#include<iostream>
using namespace std;
double func(double arr[10][2],double r,int n)
{
  double ans = 0;
  for(int i = 0; i < n; i++)
  {
    ans+= arr[i][0]/(arr[i][1]+r);
  }
  return ans;
}
double maxi(double arr[10][2],int n)
{ double m=0;
  for(int i=0;i<n;i++)
  {
    if(m<arr[i][1]){m=arr[i][1];}
  }
  return m;
}
int main()
{
  int n,col;
  cout<<"Enter the value of N and col(2 respectively):  ";
  cin>>n>>col;
  double arr[10][2];  //col == 2
  cout<<"Enter the ratio in form A and B: ";
  for (int i = 0; i < n; i++)
  {
    cin>>arr[i][0]>>arr[i][1];
  }
  double hi=maxi(arr,n),lo=0.0,mid=0.0;
  double curr,k;
  cout<<"Enter  the K value: ";
  cin>>k;
  while(hi-lo>1e-7)
  {
    mid=(hi+lo)/2;
    curr=func(arr,mid,n);
    if(curr<k)
    {
      lo = mid + 1e-7;
    }
    else
    {
      hi = mid - 1e-7;
    }
  }
  printf("%.6f",lo);
  return 0;
}

Explore

1. Quantum Mechanics relation with quantum computing

2. Unzipping Files using Python

3. An Algorithm which accepts n integer values and calculates the average and prints it.

4. How can Quantum Computing merge with Virtual Reality ? !

5. Blockchain and Cryptocurrency. And their relations with Virtual Reality !!