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[i].h2*60+agenda[i].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. 6 1710
Gaijinco wrote: 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!
The value of 1080 - 17*60 + 45 is not the same as the value of 1080 -
(17*60 + 45).
--
Pete Becker
Roundhouse Consulting, Ltd.
[snip] 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!
Well, if you are looking for the difference between the two times, why
don't you just subtract and take the absolute value?
diff(a, b) = |a-b| = |b-a| //math, not C++ 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!
This time you did the subtraction right, but you forgot to take the
absolute value. What's happening, here?!
Thanks.
Ben
"Gaijinco" <ga******@gmail.com> wrote in message
news:11*********************@f6g2000cwb.googlegrou ps.com... 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[i].h2*60+agenda[i].m2) << endl; } // time elapsed between last appointment and 18:00 cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;
This is: 1080 - (( agenda.back().h2 * 60 ) + agenda.back().m2 )
Is that what you wanted? If not, put parenthesis around your math to make
it evaluate in the correct order.
} 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;
Now this is:
((agenda.back().h2 * 60 ) + agenda.back().m2 ) - 1080 It prints the correct negative number!
What's happening, here?!
Thanks.
Jim Langston <ta*******@rocketmail.com> wrote: "Gaijinco" <ga******@gmail.com> wrote in message cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;
This is: 1080 - (( agenda.back().h2 * 60 ) + agenda.back().m2 )
Really? I thought + and - had the same precedence and
group left to right.
I could be confused though.
Steve
"Steve Pope" <sp*****@speedymail.org> wrote in message
news:e7**********@blue.rahul.net... Jim Langston <ta*******@rocketmail.com> wrote:
"Gaijinco" <ga******@gmail.com> wrote in message
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;
This is: 1080 - (( agenda.back().h2 * 60 ) + agenda.back().m2 )
Really? I thought + and - had the same precedence and group left to right.
I could be confused though.
Steve
Hmm.. true, I probably have that wrong. Which goes to prove the point, if
you want to be sure, use ()'s
Jim Langston wrote: Hmm.. true, I probably have that wrong. Which goes to prove the point, if you want to be sure, use ()'s
If you want to be sure, pay attention to the details. Stuffing in
parentheses is a sign of laziness.
--
Pete Becker
Roundhouse Consulting, Ltd. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ben |
last post by:
Hi all,
I'm trying to figure out how how complex map, filter and reduce work
based on the following piece of code from
http://www-106.ibm.com/developerworks/linux/library/l-prog.html :
...
|
by: Jason Heyes |
last post by:
The following program does not compile. Apparantly "t" is inaccessible.
#include <iostream>
using namespace std;
template <class T> class Foo
{
T t;
public:
Foo(T t_) : t(t_) { }
|
by: Prawit Chaivong |
last post by:
Hi, gurus
I've overloaded new operator. and I found that size is strange.
It's more than sizeof(myClass) 4 bytes.
Where does it come from.
I don't know the other compilers do the same thing....
|
by: dam_fool_2003 |
last post by:
#include<stdio.h>
int main(void)
{
unsigned int a=20,b=50, c = sizeof b+a;
printf("%d\n",c);
return 0;
}
out put:
24
|
by: m_a_t_t |
last post by:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on
chapter 1.5.1 and here's the program you're sposed to make:
#include <stdio.h>
/* copy input to output; 1st version */...
|
by: Harlan Messinger |
last post by:
Since operator overloads into static functions in C#, there really doesn't
need to be a connection between the types to which the operator is being
applied and the type in which the overload is...
|
by: Ennixo |
last post by:
hi,
do you know where i can find some ebooks or websites talking about C#
optimisation ?
for exemple, i just learned that ++i is faster than i++. i would like to
know more about the things...
|
by: paulo |
last post by:
Can anyone please tell me how the C language interprets the following
code:
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 10;
int x = 3;
|
by: jacob navia |
last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of
C.
All is not lost however. In the book
"Value Range Analysis of C programs" Axel Simon tries to establish a...
|
by: Alexandru Palade |
last post by:
Nothing strange about that syntax (though some spaces might helped you out).
The '&' operator is a bit-wise operator
(http://docs.python.org/ref/bitwise.html).
甜瓜 wrote:
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |