fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main() {
  7. int n; // jumlah senyawa
  8. cin >> n;
  9.  
  10. map<string, int> unsurTotal; // menyimpan total atom setiap unsur
  11.  
  12. for (int i = 0; i < n; i++) {
  13. string unsur;
  14. int jumlah;
  15. cin >> unsur >> jumlah;
  16. unsurTotal[unsur] += jumlah;
  17. }
  18.  
  19. // menampilkan hasil
  20. for (auto it : unsurTotal) {
  21. cout << it.first << " " << it.second << " ";
  22. }
  23. cout << endl;
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0s 5316KB
stdin
1 C 1 H 3 Cl 1
2 H 2 O 1
stdout
C 1