Advertisement
STANAANDREY

forked interv 10/XII/2021

Dec 10th, 2021
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. pair<int, string> parseLine(const string &line) {
  5.     int nr = 0, i;
  6.     for (i = 0; line[i] != ' '; i++) {
  7.         nr = 10 * nr + line[i] - '0';
  8.     }
  9.     return make_pair(nr, line.substr(i + 1));
  10. }
  11.  
  12. void solve(const vector<string> &lines) {
  13.     double ans = 0;
  14.     for (const auto &line : lines) {
  15.         const auto &[nr, unity] = parseLine(line);
  16.         if (unity == "kg") {
  17.             ans += nr * 1e3;
  18.         } else if (unity == "mg") {
  19.             ans += 1.0 * nr / 1e3;
  20.         } else {
  21.             ans += nr;
  22.         }
  23.     }
  24.     cout << fixed << showpoint << setprecision(5) << ans << " g" << endl;
  25. }
  26.  
  27. vector<string> read() {
  28.     vector<string> lines;
  29.     for (string line; getline(cin, line);) {
  30.         lines.push_back(line);
  31.     }
  32.     return lines;
  33. }
  34.  
  35. signed main() {
  36.     freopen("text.in", "r", stdin);
  37.     solve(read());
  38.     return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement