G
Gaijinco
I was trying to solve: acm.uva.es/p/v101/10191.html
I did this code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;
bool digit(char c)
{
return isdigit(c);
}
struct appointment
{
int h1; // beginning hour of app.
int m1; // beggining minute of app.
int h2; // ending hour of app.
int m2; // ending minute of app.
};
// compare appointments by their beggining hour
bool lt(appointment a1,appointment a2)
{
return (a1.h1)<(a2.h1);
}
int main()
{
string n; // number of appointments in a day
while(getline(cin,n))
{
vector<appointment>agenda;
int app = atoi(n.c_str());
for(int times=1; times<=app; ++times)
{
string line;
getline(cin,line);
// appointments are given in format h1:m1 h2:m2 blah, blah
appointment aux;
aux.h1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':')+1);
aux.m1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(' ')+1);
aux.h2 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':')+1);
aux.m2 = atoi(line.c_str());
agenda.push_back(aux);
}
sort(agenda.begin(),agenda.end(),lt);
// all appointments begin at 10:00 and end at 18:00
// time elapsed between 10:00 and first appointment
cout << agenda.front().h1*60+agenda.front().m1 - 600 << endl;
for(int i=0; i<agenda.size()-1; ++i)
{
// time elapsed between i-th and i+1-th appointments
cout <<
agenda[i+1].h1*60+agenda[i+1].m1)-(agenda.h2*60+agenda.m2)
<< endl;
}
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;
}
return 0;
}
My problem is with line:
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;
When the last input for the day is
XX:XX 17:45
It prints 105! As a matter of fact unless the last time is 18:00 it
always prints a wrong number!
What's more strange is if I do:
// time elapsed between last appointment and 18:00
cout << agenda.back().h2*60+agenda.back().m2-1080 << endl << endl;
It prints the correct negative number!
What's happening, here?!
Thanks.
I did this code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;
bool digit(char c)
{
return isdigit(c);
}
struct appointment
{
int h1; // beginning hour of app.
int m1; // beggining minute of app.
int h2; // ending hour of app.
int m2; // ending minute of app.
};
// compare appointments by their beggining hour
bool lt(appointment a1,appointment a2)
{
return (a1.h1)<(a2.h1);
}
int main()
{
string n; // number of appointments in a day
while(getline(cin,n))
{
vector<appointment>agenda;
int app = atoi(n.c_str());
for(int times=1; times<=app; ++times)
{
string line;
getline(cin,line);
// appointments are given in format h1:m1 h2:m2 blah, blah
appointment aux;
aux.h1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':')+1);
aux.m1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(' ')+1);
aux.h2 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':')+1);
aux.m2 = atoi(line.c_str());
agenda.push_back(aux);
}
sort(agenda.begin(),agenda.end(),lt);
// all appointments begin at 10:00 and end at 18:00
// time elapsed between 10:00 and first appointment
cout << agenda.front().h1*60+agenda.front().m1 - 600 << endl;
for(int i=0; i<agenda.size()-1; ++i)
{
// time elapsed between i-th and i+1-th appointments
cout <<
agenda[i+1].h1*60+agenda[i+1].m1)-(agenda.h2*60+agenda.m2)
<< endl;
}
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;
}
return 0;
}
My problem is with line:
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;
When the last input for the day is
XX:XX 17:45
It prints 105! As a matter of fact unless the last time is 18:00 it
always prints a wrong number!
What's more strange is if I do:
// time elapsed between last appointment and 18:00
cout << agenda.back().h2*60+agenda.back().m2-1080 << endl << endl;
It prints the correct negative number!
What's happening, here?!
Thanks.