Advertisement
Morass

Molar Mass

Jan 17th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Locale;
  3. import java.util.Scanner;
  4. class Main {
  5.     public static void main(String[] args) throws IOException{
  6.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  7.         Scanner input = new Scanner(System.in);
  8.         int T=input.nextInt();
  9.         while (T-- != 0) {
  10.             String str=input.next();
  11.             int length = str.length();
  12.             double mass = 0.0;
  13.             for (int i = 0; i < length; i++) {
  14.                 int x = 0;
  15.                 char ele = str.charAt(i);
  16.                 while ((i+1) < length && Character.isDigit(str.charAt(i+1))) {
  17.                     i++;
  18.                     x = x * 10 + (str.charAt(i) - '0');
  19.                 }
  20.                 if (x == 0) x = 1;
  21.                 mass = mass + f(ele) * x;
  22.             }
  23.             System.out.println(String.format(Locale.ENGLISH, "%.3f", mass));
  24.         }
  25.         in.close();
  26.     }
  27.     public static double f(char ch) {
  28.         if (ch == 'C') return 12.01;
  29.         else if (ch == 'H') return 1.008;
  30.         else if (ch == 'O') return 16.00;
  31.         else return 14.01;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement