fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. map<string, int> atomCount; // Menyimpan jumlah atom per unsur
  6. int n; // jumlah input
  7. cin >> n;
  8.  
  9. for (int i = 0; i < n; i++) {
  10. string elem;
  11. int count;
  12. cin >> elem >> count;
  13. atomCount[elem] += count; // menambahkan jumlah atom jika unsur sudah ada
  14. }
  15.  
  16. // Mencetak hasil sesuai urutan input pertama kali muncul
  17. for (auto &p : atomCount) {
  18. cout << p.first << " " << p.second << " ";
  19. }
  20. cout << endl;
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 5320KB
stdin
1 C 1 H 3 Cl 1
2 H 2 O 1
stdout
C 1