## 145 - Gondwanaland Telecom

Moderator: Board moderators

Archangel
New poster
Posts: 29
Joined: Wed Jun 26, 2002 9:00 am

### 145 - Gondwanaland Telecom

Are there any tricky input in this problem??...I got wrong answer in this problem...
Thanks

broderic
New poster
Posts: 34
Joined: Thu Jun 06, 2002 4:35 am
The mistake I made when I did this problem was to forget that the
length was <= 24 hours, not < 24 hours.
Did you handle the 24 hour case?
Besides that, i can't think of anything that is tricky about it.

Archangel
New poster
Posts: 29
Joined: Wed Jun 26, 2002 9:00 am
Can the input of reversed order of hour and minute be possible?? Although I can handle the 24 hours , I still got WA..

broderic
New poster
Posts: 34
Joined: Thu Jun 06, 2002 4:35 am
Hmm, I think the input is in the correct format.
What kind of algorithm are you using to determine the cost of each
call? If you are using if's, I found it can get a little messy (and easily
prone to little mistakes).

Also, did you copy the given table of prices exactly?

Broderick

Archangel
New poster
Posts: 29
Joined: Wed Jun 26, 2002 9:00 am
You guess right! I use many "if else"...My algorithm:

Code: Select all

``````           if Start_hour > End_hour
{
if Start_hour is in the day
{
if End_hour is in the day
.......(assign time of  day,night,evening)
if End_hour is in the evening
.......
if End_hour is in the night
.......
}
if Start_hour is in the evening
{
if End_hour is in the day
.......(assign time of  day,night,evening)
if End_hour is in the evening
.......
if End_hour is in the night
........
}
if Start_hour is in the night
{
if End_hour is in the day
.......(assign time of  day,night,evening)
if End_hour is in the evening
.......
if End_hour is in the night
........
}
}
else if Start_hour < End_hour
{
...........(same as above)
}
else
{
if Start_minute > End_minute
.......
else if Start_minute < End_minute
.......
else
........
}
``````
Does this kind of algorithm miss something?? And I can sure that I have copied correct prices.

10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:
This kind of algorithm itself is good and bugless, since the if-then-else conditions consider all the cases. But the disadvantage of it is VERY easy to have mistake, usually typing mistake when typing or copy-and-pasting. e.g. <= and <, i and j, etc.

broderic
New poster
Posts: 34
Joined: Thu Jun 06, 2002 4:35 am
yes, like the previous poster said, it looks like your algorithm is correct
but there could be a small error in there.

SPOILER!!!

Personally, i found this problem much easier if you create an array
with as many elements as minutes in 24hours. then, for each element,
you assign a 0 if that time is in the day, 1 if it's in the evening, and 2
if it's at night. To calculate the cost of a call, you do a loop
from the starttime to the endtime, stepping by 1 minute. at each minute,
index into your price table with the array to get the price for that minute.

Example: say we have just 3 different prices, 5, 11, 17 cents per minute
for day,evening,night respectively. and suppose just 10 minutes in
our day divided up as follows: 3-5 is the day, 6-8 is evening, and
9-2 is night.

our array would look like this:
time[0] = 2; // index the 'night' price
time[1] = 2;
time[2] = 2;
time[3] = 0; // index the day price
time[4] = 0;
etc...

to calculate the price of a call from 2 to 7, we'd loop over those values
getting (price[2] + price[0] + price[0] + price[0] + price[1]...) =
5 + 11 + 11 + 11 + 17 + ...

Easy!

Watch out for 24h days though.

This should be easy to extend to the case in the problem.

P.S. If anyone feels this is too much of a spoiler, I'll delete this post.

Archangel
New poster
Posts: 29
Joined: Wed Jun 26, 2002 9:00 am
I check many times..but still can't figure out the reason why I got WA..
Can anybody take a look of my code??....Thanks a lot!!

Code: Select all

``````#include<iostream.h>
#include<string.h>

#define day     8
#define evening 18
#define night   22

char      step,phone[8],TimeSH[2],TimeSM[2],TimeEH[2],TimeEM[2];
short int StartH,StartM,EndH,EndM;
short int hour,daysec,nightsec,eveningsec;
float     prize;

short int section(short int time)
{
if ((time >= day) && (time < evening))
return 1; /* means time is in day section */
else if ((time >= evening) && (time < night))
return 2; /* means time is in evening section */
else /* means time is in night section */
return 3;
}

void main(void)
{
cout.setf(ios::fixed);
cout.precision(2);
while(cin>>step)
{
if (step == '#')
break;
cin>>phone>>TimeSH>>TimeSM>>TimeEH>>TimeEM;
StartH = (TimeSH[0] - 48) * 10 + TimeSH [1] - 48;
StartM = (TimeSM[0] - 48) * 10 + TimeSM [1] - 48;
EndH   = (TimeEH[0] - 48) * 10 + TimeEH [1] - 48;
EndM   = (TimeEM[0] - 48) * 10 + TimeEM [1] - 48;
hour   = 24 - StartH;
if (StartH > EndH) /* start time > end time */
{
if ((hour < 3)||(hour > 16)) /* start time falls in night section */
{
if (section(EndH) == 1) /* end time falls in day section */
{
eveningsec = 0;
daysec     = (EndH - day) * 60 + EndM;
nightsec   = (day + (24 - StartH) - 1) * 60 + (60 - StartM);
}
else if (section(EndH) == 2) /* end time falls in evening section */
{
eveningsec = (EndH - evening) * 60 + EndM;
daysec     = 600;
nightsec   = (day + (24 - StartH) - 1) * 60 + (60 - StartM);
}
else /* end time falls in night section(same as start time) */
{
eveningsec = 240;
daysec     = 600;
if ((StartH < day)&&(EndH < day)) /* they all fall after 24:00 */
nightsec = (day - StartH - 1 + EndH + 2) * 60 + (60 - StartM + EndM);
else if ((StartH >= night)&&(EndH >= night)) /* they all fall before 24:00 */
nightsec = (24 - StartH - 1 + EndH - night + day) * 60 + (60 - StartM + EndM);
else /* start time falls before 24:00, end time falls after 24:00 */
{
eveningsec = 0;
daysec     = 0;
nightsec   = (24 - StartH - 1 + EndH) * 60 + (60 - StartM + EndM);
}
}
}
else if (hour < 7) /* start time falls in evening section */
{
if (section(EndH) == 1) /* end time falls in day section */
{
eveningsec = (night - StartH - 1) * 60 + (60 - StartM);
daysec     = (EndH - day) * 60 + EndM;
nightsec   = 600;
}
else if (section(EndH) == 2) /* end time falls in evening section */
{
eveningsec = (night - StartH - 1 + EndH - evening) * 60 + (60 - StartM + EndM);
daysec     = 600;
nightsec   = 600;
}
else /* end time falls in night section */
{
eveningsec = (night - StartH - 1) * 60 + (60 - StartM);
daysec     = 0;
nightsec   = (EndH + 2) * 60 + (EndM);
}
}
else /* start time falls in day section */
{
if (section(EndH) == 1) /* end time falls in day section */
{
eveningsec = 240;
daysec     = (evening - StartH - 1 + EndH - day) * 60 + (60 - StartM + EndM);
nightsec   = 600;
}
else /* end time falls in night section */
{
eveningsec = 240;
daysec     = (evening - StartH - 1) * 60 + (60 - StartM);
nightsec   = (EndH + 2) * 60 + (EndM);
}
}
}
else if (StartH < EndH) /* start time < end time */
{
if ((hour < 3)||(hour > 16)) /* start time falls in night section */
{
if (section(EndH) == 1) /* end time falls in day section */
{
eveningsec = 0;
daysec     = (EndH - day) * 60 + EndM;
nightsec   = (day - StartH - 1) * 60 + (60 - StartM);
}
else if (section(EndH) == 2) /* end time falls in evening section */
{
eveningsec = (EndH - evening) * 60 + EndM;
daysec     = 600;
nightsec   = (day - StartH - 1) * 60 + (60 - StartM);
}
else /* end time falls in night section(same as start time) */
{
if (((StartH < day)&&(EndH < day))||((StartH >= night)&&(EndH >= night))) /* they all fall after 24:00 or before 24:00 */
{
eveningsec = 0;
daysec     = 0;
nightsec   = (EndH - StartH - 1) * 60 + (60 - StartM + EndM);
}
else /* start time falls after 24:00, end time falls before 24:00 */
{
eveningsec = 240;
daysec     = 600;
nightsec   = (day - StartH - 1 + EndH - night) * 60 + (60 - StartM + EndM);
}
}
}
else if (hour < 7) /* start time falls in evening section */
{
if (section(EndH) == 2) /* end time falls in evening section */
{
eveningsec = (EndH - StartH - 1) * 60 + (60 - StartM + EndM);
daysec     = 0;
nightsec   = 0;
}
else /* end time falls in night section */
{
eveningsec = (night - StartH - 1) * 60 + (60 - StartM);
daysec     = 0;
nightsec   = (EndH - night) * 60 + (EndM);
}
}
else /* start time falls in day section */
{
if (section(EndH) == 1) /* end time falls in day section */
{
eveningsec = 0;
daysec     = (EndH - StartH - 1) * 60 + (60 - StartM + EndM);
nightsec   = 0;
}
else if (section(EndH) == 2) /* end time falls in evening section */
{
eveningsec = (EndH - evening) * 60 + (EndM);
daysec     = (evening - StartH - 1) * 60 + (60 - StartM);
nightsec   = 0;
}
else /* end time falls in night section */
{
eveningsec = 240;
daysec     = (evening - StartH - 1) * 60 + (60 - StartM);
nightsec   = (EndH - night) * 60 + (EndM);
}
}
}
else /* start time = end time */
{
if (StartM < EndM) /* start minute < end minute */
{
eveningsec = 0;
daysec     = 0;
nightsec   = 0;
if (section(StartH) == 1) /* both fall in day section */
daysec     = EndM - StartM;
else if (section(StartH) == 2) /* both fall in evening section */
eveningsec = EndM - StartM;
else /* both fall in night section */
nightsec   = EndM - StartM;
}
else if (StartM > EndM) /* start minute > end minute */
{
eveningsec = 240;
daysec     = 600;
nightsec   = 600;
if (section(StartH) == 1) /* both fall in day section */
daysec     = 600 - (StartM - EndM);
else if (section(StartH) == 2) /* both fall in evening section */
eveningsec = 240 - (StartM - EndM);
else /* both fall in night section */
nightsec   = 600 - (StartM - EndM);
}
else /* start minute = end minute */
{
eveningsec = 240;
daysec     = 600;
nightsec   = 600;
}
}
switch (step)
{
case 'A':
prize = 0.1 * daysec + 0.06 * eveningsec + 0.02 * nightsec;
break;
case 'B':
prize = 0.25 * daysec + 0.15 * eveningsec + 0.05 * nightsec;
break;
case 'C':
prize = 0.53 * daysec + 0.33 * eveningsec + 0.13 * nightsec;
break;
case 'D':
prize = 0.87 * daysec + 0.47 * eveningsec + 0.17 * nightsec;
break;
default: /* case E */
prize = 1.44 * daysec + 0.8 * eveningsec + 0.3 * nightsec;
}
cout.width(10);
cout<<phone;
cout.width(5);
cout<<daysec;
cout.width(7);
cout<<eveningsec;
cout.width(6);
cout<<nightsec;
cout.width(3);
cout<<step;
cout.width(8);
cout<<prize<<endl;
}
}
``````

Lawrence
New poster
Posts: 17
Joined: Sat Feb 09, 2002 2:00 am
Location: China
Contact:
I am quite confused by ur code.
[cpp]StartH = (TimeSH[0] - 48) * 10 + TimeSH [1] - 48;
StartM = (TimeSM[0] - 48) * 10 + TimeSM [1] - 48;
EndH = (TimeEH[0] - 48) * 10 + TimeEH [1] - 48;
EndM = (TimeEM[0] - 48) * 10 + TimeEM [1] - 48; [/cpp]
What is this kind of things? I figure the input times work on their own.

If you want to have a look at my code, send me an email pls.

Archangel
New poster
Posts: 29
Joined: Wed Jun 26, 2002 9:00 am
Sorry~my code is not clear..In my code
TimeSH is a one dimension array to store the input data (starting hour)
TimeSM(starting minute),TimeEH(ending hour),TimeEM(ending minute) are the same. Because these four array's type is "char", so I must change
the type so that I can assign the right value to StartH,StartM,EndH,EndM.
The code you confused is just the type transformation.(like the function "atoi" in c library). Because when the input data is a char
,it will store ASCII code in the variable as you assign. For example:
a = '1', the variable will contain the value of 1's ASCII code 49.

Lawrence
New poster
Posts: 17
Joined: Sat Feb 09, 2002 2:00 am
Location: China
Contact:
1. Do you need to null terminate your 'phone' variable?

2. You need to print 0.60 if the money is 0.6

Archangel
New poster
Posts: 29
Joined: Wed Jun 26, 2002 9:00 am
Null terminate?? I think my code doesn't need it. Because phone is a array with fixed 8 element. Every time if you read in a phone number, the
phone number will overwrite the phone array completely. And my code will output 0.6 as 0.60 because of this line "cout.precision(2)". I still don't know why I can't get accept....

Lawrence
New poster
Posts: 17
Joined: Sat Feb 09, 2002 2:00 am
Location: China
Contact:
Oh, that's it. Your phone array should have a size of 9, to include the null-terminator.

And your program prints 0.6, not 0.60 in my computer. Dunno if it is the problem.

Archangel
New poster
Posts: 29
Joined: Wed Jun 26, 2002 9:00 am
You wisdom really do me a great favor. I finally got accept!! Thank you for paying so much attention to my problem

Archangel
New poster
Posts: 29
Joined: Wed Jun 26, 2002 9:00 am
Your wisdom really do me a great favor. I finally got accept!! Thank you for paying so much attention to my problem