Friday, March 25, 2016

URI Online Judge Solution | 1005 Average 1 Using C, C++ and Java Language

Read two floating points' values of double precision A and B, corresponding to two student's grades. After this, calculate the student's average, considering that grade A has weight 3.5 and B has weight 7.5. Each grade can be from zero to ten, always with one digit after the decimal point. Don’t forget to print the end of line after the result, otherwise you will receive “Presentation Error”. Don’t forget the space before and after the equal sign.

Input

The input file contains 2 floating points' values with one digit after the decimal point.

Output

Print MEDIA(average in Portuguese) according to the following example, with 5 digits after the decimal point and with a blank space before and after the equal signal.
Input SamplesOutput Samples
5.0
7.1
MEDIA = 6.43182
0.0
7.1
MEDIA = 4.84091
10.0
10.0
MEDIA = 10.00000


Solution in C language:

#include<stdio.h>

int main(){

   float A,B,med;

   scanf("%f %f",&A,&B);

   med=((A*3.5)+(B*7.5))/(3.5+7.5);

   printf("MEDIA = %.5f\n",med);

   return 0;

}


 Solution in C++ language:


#include<stdio.h>
#include<iomanip>
using namespace std;
int main(){
   float A,B,med;
   cin >> A >> B;
   med=((A*3.5)+(B*7.5))/(3.5+7.5);
   cout << "MEDIA = "<< fixed << setprecision(5) << med << endl ;
   return 0;
}



Solution in java language: 

import java.util.Scanner;

public class URI_1005 {

    public static void main(String[] args){

            float A, B, med;
            Scanner sc = new Scanner(System.in);
            A = sc.nextFloat();
            B = sc.nextFloat();
            med = (float) (((A * 3.5) + (B * 7.5)) / (3.5 + 7.5));
            String media = String.format("MEDIA = %,.5f", med);
            System.out.print(media +"\n");

    }

}

This is a simple average problem. Just take two input from user and then print the average writing the word "MEDIA =".

Obviously, you have to give a new line at the end of the code. The new line formate in C and java language is "\n". and in  C++ language it can be given by two option like---> "\n" or endl. For C++ lool at the line: 


cout << "MEDIA = "<< fixed << setprecision(5) << med << endl ;

Here, we need a float value of 5 digits after the decimal point.So, here after cout we need to fixed the result set  fixed  and set the precision 5 by setprecision(5)   .And then print the media and which will must print the absolute  result.
And for java look at the line'
String media = String.format("MEDIA = %,.5f", med);
This is the String format method... Which detail is :
Examples: 

  • System.out.printf("Total is: $%,.2f%n", dblTotal);
  •  System.out.printf("Total: %-10.2f: ", dblTotal);
  •  System.out.printf("% 4d", intValue); 
  • System.out.printf("%20.10s\n", stringVal);
  •  String s = "Hello World";
  •  System.out.printf("The String object %s is at hash code %h%n", s, s);
  • String grandTotal = String.format("Grand Total: %,.2f", dblTotal);

I think, there will be no problem to clear the above problem. Since if you face any problem to solve the question , you can better comment below.

The main link of the problem is : https://www.urionlinejudge.com.br/judge/en/problems/view/1005










4 comments:
Write comments
  1. Replies
    1. https://www.youtube.com/watch?v=slFqL86q3EA&ab_channel=TylerDeWitt

      Delete
  2. Replies
    1. https://www.youtube.com/watch?v=slFqL86q3EA&ab_channel=TylerDeWitt

      Delete

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.