Sfoglia il codice sorgente

化学分子量:枚举、字符串处理技巧

zj 5 anni fa
parent
commit
d0ad96ca61
1 ha cambiato i file con 37 aggiunte e 0 eliminazioni
  1. 37 0
      C.cpp

+ 37 - 0
C.cpp

@@ -0,0 +1,37 @@
+#include <bits/stdc++.h>
+
+using namespace std;
+map<char, int> M;
+
+void fill() {
+    M['H'] = 1;
+    M['C'] = 12;
+    M['N'] = 14;
+    M['O'] = 16;
+    M['F'] = 19;
+    M['P'] = 31;
+    M['S'] = 32;
+    M['K'] = 39;
+}
+
+int main() {
+    fill();
+    int T;
+    cin >> T;
+    while (T--) {
+        string str;
+        cin >> str;
+        int tot = 0;
+        for (size_t i = 0; i < str.length(); i++) {
+            if (isalpha(str[i])) {
+                if (i == str.length() - 1 || isalpha(str[i + 1])) {
+                    tot += M[str[i]];
+                } else {
+                    tot += M[str[i]] * (str[i + 1] - '0');
+                }
+            }
+        }
+        cout << tot << endl;
+    }
+    return 0;
+}