10683  The decadary watch
Moderator: Board moderators
10683  The decadary watch
Easy problem but got wa. I think the problem is the precision. How do I get the digits of the result precisely? Or are there some critical input?
I don't think there is any critical input.
The main Idea is that the number of MMs in day of our system is 125/108 time small then in new time system.You must find number of MMs from 00:00:00 to the given time in our time system(for example P). And p*125/108rd MM will be in new time system.Than you must make it to seconds, minutes, hours.Besouse p*125/108 is not integer we must take trunc(p*125/108).In problem discription there is written that we must take TRUNCATION.
The main Idea is that the number of MMs in day of our system is 125/108 time small then in new time system.You must find number of MMs from 00:00:00 to the given time in our time system(for example P). And p*125/108rd MM will be in new time system.Than you must make it to seconds, minutes, hours.Besouse p*125/108 is not integer we must take trunc(p*125/108).In problem discription there is written that we must take TRUNCATION.
someone who like to solve informatic problems.
http://acm.uva.es/cgibin/OnlineJudge?AuthorInfo:29650
http://acm.uva.es/cgibin/OnlineJudge?AuthorInfo:29650

 Guru
 Posts: 834
 Joined: Wed May 29, 2002 4:11 pm
 Location: Wroclaw, Poland
 Contact:
I use the same approach and got Acc at first attempt ... I don't use floor, but integral cast
Best regards
DM
PS. I also think that this problem has no critical inputs, but I may be wrong
Best regards
DM
PS. I also think that this problem has no critical inputs, but I may be wrong
If you really want to get Accepted, try to think about possible, and after that  about impossible ... and you'll get, what you want ....
Born from ashes  restarting counter of problems (800+ solved problems)
Born from ashes  restarting counter of problems (800+ solved problems)
10683 > Problem C  The decadary watch
Greetings!
I've read about this problen in the other post, but still don't get it's logic.
Will anyone please tell me what's wrong with my code?
Thanks in advance!
[pascal]{ Bernardo E. L
I've read about this problen in the other post, but still don't get it's logic.
Will anyone please tell me what's wrong with my code?
Thanks in advance!
[pascal]{ Bernardo E. L
Last edited by _.B._ on Mon Aug 16, 2004 2:55 am, edited 2 times in total.
_.

 Guru
 Posts: 1080
 Joined: Thu Dec 19, 2002 7:37 pm
You are loosing precision by using the real type for Hora1. The function trunc() can therefor give too low answers. It is better to use integer types all the way.
In your program I used QWord for both Hora1 and Hora2 and then changed two lines:[pascal] Hora1:=((((((HH*60)+MM)*60)+SS)*100)+CC)*100/CienPHora;
Hora2:=trunc(CienPDeci*Hora1/100);[/pascal]into:[pascal]
Hora1:=(((((HH*60)+MM)*60)+SS)*100)+CC;
Hora2:=Hora1*CienPDeci div CienPHora;
[/pascal]and got Accepted.
Another method is to avoid the function trunc() from giving too low answers by adding a small 'fudge factor' before truncation.
Changing the line:[pascal] Hora2:=trunc(CienPDeci*Hora1/100);
[/pascal]to:[pascal] Hora2:=trunc(CienPDeci*Hora1/100+0.000000001);
[/pascal]will also get you accepted.
In your program I used QWord for both Hora1 and Hora2 and then changed two lines:[pascal] Hora1:=((((((HH*60)+MM)*60)+SS)*100)+CC)*100/CienPHora;
Hora2:=trunc(CienPDeci*Hora1/100);[/pascal]into:[pascal]
Hora1:=(((((HH*60)+MM)*60)+SS)*100)+CC;
Hora2:=Hora1*CienPDeci div CienPHora;
[/pascal]and got Accepted.
Another method is to avoid the function trunc() from giving too low answers by adding a small 'fudge factor' before truncation.
Changing the line:[pascal] Hora2:=trunc(CienPDeci*Hora1/100);
[/pascal]to:[pascal] Hora2:=trunc(CienPDeci*Hora1/100+0.000000001);
[/pascal]will also get you accepted.
Thanks!
Little Joey, once again, thanks for taking the time to answer!
Some times it's easier to code a program, than to read and correct one.
You rule!
I find the second option you gave really interesting, and gives a faster execution time (just a bit).
Now, how do you know how small the 'fudge factor' should be? (0.000000001 this time)
Thanks!, and keep posting!
Some times it's easier to code a program, than to read and correct one.
You rule!
I find the second option you gave really interesting, and gives a faster execution time (just a bit).
Now, how do you know how small the 'fudge factor' should be? (0.000000001 this time)
Thanks!, and keep posting!
_.

 Guru
 Posts: 1080
 Joined: Thu Dec 19, 2002 7:37 pm
I guess it's something of an art
It depends on a lot of things:
 the precision of the internal representation of a real;
 the precision of your calculation (order of operators);
 the range of integers you get after truncation;
 the luck that the input set doesn't contain cases for which it fails.
And it's not always possible. Best is trial and error until you get Accepted...
Therefor an all integer solution, if possible, should be preferred, because you get "infinite precision". And it can be as fast as, or almost always faster than a solution with reals, if you can get your algorithm right.
It depends on a lot of things:
 the precision of the internal representation of a real;
 the precision of your calculation (order of operators);
 the range of integers you get after truncation;
 the luck that the input set doesn't contain cases for which it fails.
And it's not always possible. Best is trial and error until you get Accepted...
Therefor an all integer solution, if possible, should be preferred, because you get "infinite precision". And it can be as fast as, or almost always faster than a solution with reals, if you can get your algorithm right.
Na' guara'!!
Well, thanks again for the info
You are right, it'll be better to work with integers, with a good algo
Keep posting!
Thanks!
You are right, it'll be better to work with integers, with a good algo
Keep posting!
Thanks!
_.
10683
Greatings!
I have attempted several times, but i still got WA..
i really dont know where code is wrong...
my code is following..
would you tell me where is wrong ?
if any code is wrong, tell me why it is wrong..
thanks for reading.
I have attempted several times, but i still got WA..
i really dont know where code is wrong...
my code is following..
Code: Select all
char input[9], buf[12];
int hh, mm, ss, cc, sum;
float sumf;
while( scanf("%s", input) != 1 )
{
sprintf(buf, "%c%c %c%c %c%c %c%c",
input[0], input[1], input[2], input[3],
input[4], input[5], input[6], input[7]);
sscanf(buf, "%d %d %d %d", &hh, &mm, &ss, &cc);
sum = (hh*3600 + mm * 60 + ss) * 100 + cc;
sumf = (float)sum * (125.0 / 108);
printf("%07d\n", (int)sumf);
}
if any code is wrong, tell me why it is wrong..
thanks for reading.
First, you can avoid all floating point problems by using integers, instead of floating point numbers (sum*125/108 works and 125*sum still fits in 32 bit signed integer)
Second, if you use floating point numbers, use double instead of float. (float has only a precision of about 7 decimals, which is critical in this case)
Third, when converting double's to integers, I always do something like to avoid small rounding errors.
Second, if you use floating point numbers, use double instead of float. (float has only a precision of about 7 decimals, which is critical in this case)
Third, when converting double's to integers, I always do something like
Code: Select all
(int)(floor(f+1e10)+1e10)