UVa1586

題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4461

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;

double getMass(char c){
if(c == 'C') return 12.01;
if(c == 'H') return 1.008;
if(c == 'O') return 16.00;
if(c == 'N') return 14.01;
return 0.0;
}

int main(){
ios::sync_with_stdio(0);cin.tie(0);
int t;
cin >> t >> ws;
string text;
map<char, int> counts;
while(t--){
counts.clear();
getline(cin, text);
int n = text.length();
int i = 0;
while(i < n){
int num = 0;
char c = text[i++];
while(i < n && isdigit(text[i])){
num = num*10 + (text[i] - '0');
i++;
}
if(num == 0) num=1;
counts[c] += num;
}
double total = 0;
total += counts['C'] * getMass('C');
total += counts['H'] * getMass('H');
total += counts['O'] * getMass('O');
total += counts['N'] * getMass('N');
cout << fixed << setprecision(3) << total << '\n';
}
return 0;
}