Question address: http://acm.hdu.edu.cn/showproblem.php? PID = 1861 one postgraduate entrance exam computer question = B
Train of Thought: place the information in the struct, then exist in the vector, and finally scan to n = 0 for statistics, output, and clear the vector.
It should be noted that a single ship may be rented twice, so when scanning op = 'E', it should be handled immediately.
2. When op = 'E' is scanned, check whether there is a s record. If no record is found, it will not be processed.
3. I borrowed one minute from-> ~ Instead of 0 minutes
4. Use stringstream to convert strings and integers. # include <sstream>
Code:
# Include <iostream> # include <string> # include <vector> # include <map> # include <sstream> # include <cmath> using namespace STD; struct info {int number; char op; string time ;}; int stringtoint (string s) {int ans; stringstream SS; SS <s; SS> ans; return ans;} int main () {int N; vector <info> V; while (CIN> N) {info theinfo; char op; CIN> op; string time; CIN> time; if (n =-1) break; If (n = 0) {int COUNT = 0; double AV E = 0; Map <int, string> Start; Map <int, string> end; For (INT I = 0; I <v. size (); I ++) {If (V [I]. OP ='s ') Start [V [I]. number] = V [I]. time; else if (V [I]. OP = 'E' & start. find (V [I]. number )! = Start. end () {count ++; string starthour = start. find (V [I]. number)-> second. substr (0, 2); string startminute = start. find (V [I]. number)-> second. substr (3, 2); string endhour = V [I]. time. substr (0, 2); string endminute = V [I]. time. substr (3, 2); // minute-level computing caution if (startminute <= endminute) Ave + = stringtoint (endminute)-stringtoint (startminute); else {Ave + = stringtoint (endminute) -stringtoint (startminute) + 1;} Ave + = (stringto INT (endhour)-stringtoint (starthour) * 60 ;}} v. Clear (); If (count! = 0) Ave = Ave/count; cout <count <"" <floor (AVE) <Endl;} else {theinfo. time = time; theinfo. number = N; theinfo. OP = op; V. push_back (theinfo );}}}