You have been given ‘N’ number of spherical Balloons of different radius when filled. You have to fill one balloon per day and the last balloon will be filled on Nth day. There is some rate of air reduction ‘K’ per day from each balloon. Fill the balloons in such an order so that the sum of the volume of all the balloons is maximum on the day when all the balloons are filled.
Input Format:
First Line is an integer N giving the number of balloons. Second line gives space separated N positive real numbers with up to 1 decimal place giving the radii of the balloons. Third line gives K, the rate of reduction in the volume of air as a percentage.
Output Format:
Maximum sum of volumes of all the balloons on the Nth day when all the balloons are filled. Take 3.14 as the value of PI and give the answer to two decimal places (truncated by ignoring all the decimals from third onwards). Note that the truncation should happen only after computing the volume of all the balloons on the final day to maximum precision.
Constraints:
Number of balloons <= 10 Radius of balloons <=200
Example 1
Input:
5
8 4 6 10 3 1 0
Output:
7117.88
Explanation:
If we fill the balloons in the order 3, 4, 6, 8, 10, their volumes on the fifth day are respectively 7 4.165544 1 95.33312 7 32.4992 1929.216 4 186.666667 A nd their sum is 7117.880531. Truncating the value two decimal places, we obtain 7117.88
Example 2
Input:
7
3 .5 9 4 6.6 7 11 9.1 1 2.5
Output:
12555.35
Explanation:
If we inflate the balloons in the order 3.5 4 6.6 7 9 9.1 11, their volumes on the seventh day would be respectively 8 0.56025567 137.4322396 705.5574848 9 62.0256771 2 336.74875 2 760.581763
5572.453333 The sum of these volumes is 12555.3595 and truncating to two decimal places, we obtain 12555.35
Solution: In C
#include <stdio.h>
double rate(double y,float m,int k)
{
for(int i=k;i>=1;i--)
{
y=y-y*(m/100);
}
return y;
}
int main() {
int n,i,j;
double v,b[1000],t,v1,sum=0;
float a[1000],x;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",&a[i]);
scanf("%f",&x);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
for(i=0;i<n;i++)
{
v=0;
v=(4*3.14*a[i]*a[i]*a[i])/3;
b[i]=rate(v,x,n-(i+1));
}
for(i=0;i<n;i++)
sum=sum+b[i];
printf("%.2f ",sum);
return 0;
}
Solution: In Java
import java.util.*;
class Main{
public static double rate(double y,float m,int k)
{
for(int i=k;i>=1;i--)
{
y=y-y*(m/100);
}
return y;
}
public static void main(String[] args) {
int n,i,j;
double v,t,v1,sum=0;
double[] b=new double[1000];
float[] a=new float[1000];
float x;
Scanner ip=new Scanner(System.in);
n=ip.nextInt();
for(i=0;i<n;i++)
a[i]=ip.nextFloat();
x=ip.nextFloat();
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=(float)t;
}
for(i=0;i<n;i++)
{
v=0;
v=(4*3.14*a[i]*a[i]*a[i])/3;
b[i]=rate(v,x,n-(i+1));
}
for(i=0;i<n;i++)
sum=sum+b[i];
System.out.printf("%.2f ",sum);
}
}
Solution: In Python 3
def rate(y,m,k):
for i in range(k):
y=y-y*(m/100)
return y
b=[]
n=int(input())
a=list(map(int,input().split()))
x=a[-1]
a = a[:-1]
a.sort()
for i in range(0,n):
v=0
v=(4.0*3.14*a[i]*a[i]*a[i])/3
b.append(rate(v,x,n-(i+1)))
print("{:.2f}".format(sum(b)))
Also Checkout