## 10784 - Diagonals

Moderator: Board moderators

### 10784 - Diagonals

I am trying this for ages now ...

Code: Select all
`...    long long int d, n;...      double m;.../*  This is where I calculate the answer, ndia = n*(n-3)/2, ** frame a quadriatic, get the roots, take the +ve one */       m = (3.0 + sqrt( 9.0 + (d*8.0)))/2.0;    /* and output */    printf(".0lf", m+0.5);`
sumankar
A great helper

Posts: 288
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta

Precision error.
I used +0.5 as theshold when I need to round a "normal" floating point number(say, 3.4, 1.2, etc.) to integer.
If a floating point number is storing a number, which should be an integer in theory, I will use much smaller theshold like +1e-10. You will get AC by trying different combination of theshold value and ceil() or floor().
Last edited by .. on Sat Mar 12, 2005 2:42 pm, edited 1 time in total.
My signature:
We can learn much more in discussion than reading source code.
• I HATE testing account.
• Don't send me source code for debug.
..
A great helper

Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

I solved this approximating the value of N via binary search..., i know this is not the best method but i am not so good at math but it worked ok...,
neno_uci
Experienced poster

Posts: 104
Joined: Sat Jan 17, 2004 12:26 pm
Location: Cuba

Thanks, but
.. wrote:Precision error.
I used +0.5 as theshold when I need to round a "normal" floating point number(say, 3.4, 1.2, etc.) to integer.
If a floating point number is storing a number, which should be an integer in theory, I will use much smaller theshold like +1e10. You will get AC by trying different combination of theshold value and ceil() or floor().

+1e10 is small?
Code: Select all
`#define EPS = 1e-8m = (3.0 + sqrt( 9.0 + (d*8.0)))/2.0;         frac = modf(m, &whole);        if ( frac < EPS )            ;        else            whole++;        printf("Case %d: %.0lf\n", k, whole);`

doesn't work either.
Suman.
sumankar
A great helper

Posts: 288
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta

Sorry typo, should be 1e-10.
fmod() sucks, I never use it. And I already say using ceil or floor is enough.

The problem of your code is
printf(".0lf", m+0.5);
People think it will work perfectly in rounding, but it doesn't.
My signature:
We can learn much more in discussion than reading source code.
• I HATE testing account.
• Don't send me source code for debug.
..
A great helper

Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

.. - I resign.Can you give me how to do it?

I tried everything, in my capacity(rather limited, you see...)

Suman
sumankar
A great helper

Posts: 288
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta

This is what I used in my AC code:
Code: Select all
` printf ("Case %d: %d\n", t++,          (int)ceil((3.0+sqrt(9.0+8.0*N))/2.0-1e-10));`
Last edited by .. on Sat Mar 12, 2005 3:15 pm, edited 1 time in total.
My signature:
We can learn much more in discussion than reading source code.
• I HATE testing account.
• Don't send me source code for debug.
..
A great helper

Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

All of which gives a WA ...

I 'll go crazy.

Suman.
sumankar
A great helper

Posts: 288
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta

If the line of AC code can't give you AC, then you must have some other problems.

P.S. It seems that I don't remember the detail correctly, the second method in my last post is wrong.
My signature:
We can learn much more in discussion than reading source code.
• I HATE testing account.
• Don't send me source code for debug.
..
A great helper

Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Code: Select all
`scanf() == 1 && d `

was the culprit .

Here is the culprit, may be you can find something in here...
Suman.
sumankar
A great helper

Posts: 288
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta

The mistake is obvious......
Run the sample input again........
My signature:
We can learn much more in discussion than reading source code.
• I HATE testing account.
• Don't send me source code for debug.
..
A great helper

Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

..

Thanks a zillion tonnes ... I should have killed myself by now for that stupid^inf bug.

Suman.
sumankar
A great helper

Posts: 288
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta

### WHY WA

WHY WA
Code: Select all
`Removed after AC}`
Last edited by Debashis Maitra on Fri Dec 22, 2006 6:38 pm, edited 1 time in total.
Akash chhoyar swopno
Dream to touch the sky

Debashis Maitra
Learning poster

Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka

Debashis, your code is correct, except two things.

1. Case ('C' capital).
2. The problem states...
Input is terminated by a line containing a zero

So, if you get a 0 then you can finish processing.

Hope you get accepted.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

Thanx Jan Bhai

that was a silly mistake
Akash chhoyar swopno
Dream to touch the sky

Debashis Maitra
Learning poster

Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka

Next