Monday, March 28, 2016

URI 1182 Solution in java language


URI Online Judge | 1182

Column in Array

By Neilor Tonin, URI  Brazil
Timelimit: 1
Your job in this problem is to read a number that is a column of an array where an operation will be performed, an uppercase character, indicating the operation to be performed and all elements of a bidimentional arrayM[12][12]. Then, you have to calculate and print the sum or average of all elements within the green area according to the case. The following figure illustrates the case when is entered the number 5 to the array column, showing all elements that must be considered in the operation.

Input

The first line of the input contains a simple integer C (0 ≤ C ≤ 11) indicating the column to be considered in the operation. The second line of the input contains a single uppercase character T ('S' or 'M'), indicating the operation Sum or Average (Média in portuguese) to be performed with the elements of the array. Follow 144 floating-point numbers of the array.

Output

Print the calculated result (sum or average), with one digit after the decimal point.
Input SampleOutput Sample
5
S
1.0
-3.5
2.5
4.1
...
12.6
Solution in java language :

  1. import java.io.IOException;
  2. import java.util.Scanner;
  3.  
  4. /**
  5.  * IMPORTANT: 
  6.  *      O nome da classe deve ser "Main" para que a sua solução execute
  7.  *      Class name must be "Main" for your solution to execute
  8.  *      El nombre de la clase debe ser "Main" para que su solución ejecutar
  9.  */
  10. public class Main {
  11.  
  12.     public static void main(String[] args) throws IOException {
  13.  
  14.        int lineNo;
  15.         float sum = 0, avg = 0;
  16.         Scanner sc = new Scanner(System.in);
  17.         float M[][] = new float[12][12];
  18.         String option ;
  19.         lineNo = sc.nextInt();
  20.         option = sc.next();
  21.         //take input in the array
  22.         for (int i = 0; i < 12; i++) {
  23.             for (int j = 0; j < 12; j++) {
  24.                 M[i][j] = sc.nextFloat();
  25.             }
  26.         }

  27.         //logic for summation or average
  28.         //Now find the line No which we've just scanned

  29.         for (int i = 0; i < 12; i++) {   //It's only check the specific  column no
  30.             
  31.                 sum +=  M[i][lineNo];
  32.             
  33.         }
  34.         avg = sum / 12;
  35.         if(option.equals("S")){
  36.             System.out.printf("%.1f\n", sum);
  37.         }else if(option.equals("M")){
  38.             System.out.printf("%.1f\n", avg);
  39.         }
  40.  
  41.     }
  42.  
  43. }


View in dropbox :

See Full Source code









No comments:
Write comments

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.