+ 1

Test Case 3 & 4 Failed.

#include <iostream> #include <sstream> #include <vector> using namespace std; /* Link to Safety Deposit Boxes: https://www.sololearn.com/coach/17?ref=app */ int main(){ string items; getline(cin, items); string myItem; cin>>myItem; stringstream ss(items); string token; vector<string> vectItem; //tokenize or get characters of ss then split once delimiter ',' is encountered while (getline(ss,token, ',')){ vectItem.push_back(token); //store tokenized ss } int itmqty = 1, time = 5; for(int i=0;i<vectItem.size();i++){ time *= itmqty; if(vectItem.at(i)==myItem){ break; } itmqty++; } cout<<time; return 0; }

25th Jul 2024, 2:45 PM
Pat
6 Answers
+ 4
Suppose the item is at index 2 (3. item). Then your code computes in total (((5 * 1) * 2) * 3)
25th Jul 2024, 4:18 PM
Lisa
Lisa - avatar
+ 2
Great, that you fixed it! You could also do the multiplication only once – after the loop.
25th Jul 2024, 5:21 PM
Lisa
Lisa - avatar
+ 2
time += 5 placed above the if statement would have been enough, and then you can remove the itmqty++ variable.
25th Jul 2024, 7:16 PM
Jan
Jan - avatar
+ 1
Lisa Ooh. Got it! I revised my code and accomplished the task. Thank you! int itmqty = 1, min = 5, time = 0; for(int i=0;i<vectItem.size();i++){ time = itmqty * min; if(vectItem.at(i)==myItem){ break; } itmqty++; }
25th Jul 2024, 4:48 PM
Pat
+ 1
Lisa Done. Your suggestion is much better. Thanks again! int itmqty = 1, min = 5, time = 0; for(int i=0;i<vectItem.size();i++){ if(vectItem.at(i)==myItem){ break; } itmqty++; } time = itmqty * min; cout<<time;
25th Jul 2024, 5:54 PM
Pat
+ 1
Jan Ooh. Right. Less code, less mistake. Thank you. Here's the revised code: int min = 5, time = 0; for(int i=0;i<vectItem.size();i++){ time += min; if(vectItem.at(i)==myItem){ break; } }
25th Jul 2024, 7:27 PM
Pat