545 - Heads
Moderator: Board moderators
malloc() declares memory but not initializes it. So, you must initialize y[0] before using it. Hope it helps.
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 10
- Joined: Tue Aug 07, 2007 7:33 pm
-
- Experienced poster
- Posts: 145
- Joined: Thu Aug 14, 2003 8:42 am
- Location: Mountain View, California
- Contact:
Re: 545
I would recommand you to use interget to substitute double and perform exact calculation, since it maybe lost precison due to the using of double.amine.hamdaoui wrote:Why do i get WA. I use exact calculation. I have tested my code for all the tests in this topic, and it match!!!!Code: Select all
#include <stdio.h> #include <stdlib.h> int main() { int i,j,r,n,*y,a; double *x; y = (int *) malloc(sizeof(int )*1000001); x = (double *) malloc(sizeof(double)*1000001); x[0] = 1; y[0] = 0; for(j=1 ; j<1000001 ; j++) { x[j] =x[j-1]/2.0; if(x[j]<1) { x[j] *=10.0; y[j]=y[j-1]+1; } else y[j]=y[j-1]; } scanf("%d",&r); for(i=1 ; i<=r ; i++) { scanf("%d",&n); printf("2^-%d = %1.3fE-%d\n",n,x[n],y[n]); } return 0; }
P.S. After two times W.A. by using the formula of 474, I decided to use exact calculation by interger and got A.C. finally.
![:D](./images/smilies/icon_biggrin.gif)
Have you ever...
- Wanted to work at best companies?
- Struggled with interview problems that could be solved in 15 minutes?
- Wished you could study real-world problems?
Re: 545
I solved both 545 and 474 without log or log10 or pow or bigint. For 474, after you calculate the result, use the following snippet to output your result.
cout << showpoint << setprecision(4) << result;
The correct output for 6 is 1.562e-2 and not 1.563e-2 for 474. This is because the number is not rounded in this problem.
For 545, I got Wrong Answer thrice before I got Accepted. After I got Accepted, I thought I would compare the output of my programs and I wanted to post here some special cases, but guess what? The two outputs incisively matched on codeblocks. If you want to get accepted then one hint (I got accepted after adding 3 lines):
Consider the numbers 1.11450, 1.11550, 1.11650 and 1.11650.
Say x = 1.11450. Now if you print x with %0.3lf then the outputs for the four numbers will be:
1.115, 1.115, 1.117 and 1.117
Modify your code so that the output for these four numbers are instead:
1.115, 1.116, 1.117 and 1.118
Even after this modification, all the values from 1 to 9000 still remain unchanged(at least on my compiler) but for some eerie reason, the online judge doesn't accept without this rounding alteration.
This should help you![:)](./images/smilies/icon_smile.gif)
cout << showpoint << setprecision(4) << result;
The correct output for 6 is 1.562e-2 and not 1.563e-2 for 474. This is because the number is not rounded in this problem.
For 545, I got Wrong Answer thrice before I got Accepted. After I got Accepted, I thought I would compare the output of my programs and I wanted to post here some special cases, but guess what? The two outputs incisively matched on codeblocks. If you want to get accepted then one hint (I got accepted after adding 3 lines):
Consider the numbers 1.11450, 1.11550, 1.11650 and 1.11650.
Say x = 1.11450. Now if you print x with %0.3lf then the outputs for the four numbers will be:
1.115, 1.115, 1.117 and 1.117
Modify your code so that the output for these four numbers are instead:
1.115, 1.116, 1.117 and 1.118
Even after this modification, all the values from 1 to 9000 still remain unchanged(at least on my compiler) but for some eerie reason, the online judge doesn't accept without this rounding alteration.
This should help you
![:)](./images/smilies/icon_smile.gif)
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson
Re: 545
I honestly do not know what's wrong with my code. I used the square and double method, which worked perfectly well on 474, and is failing hard here. Eventually I took the suggestion of using BigInt, and got it accepted. I still have no idea what's wrong though, since both code gives the same output for all 9000 values.
Re: 545 - Heads
Got accepted with log10() and pow() in 0.015. There is no need in BigInt.Dominik Michniewski wrote:try to use log2() instead of log10() .... log2() means log(), I think .....
I remember, that it was difference in rounding ....
Dominik Michniewski
![:)](./images/smilies/icon_smile.gif)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
-
- Experienced poster
- Posts: 139
- Joined: Wed May 18, 2011 3:04 pm
Re: 545 - Heads
The format of Sample Input is wrong, there is no r in first line.
metaphysis: http://uhunt.onlinejudge.org/id/95895
My solutions for UVa problems: https://github.com/metaphysis/Code.
My solutions for UVa problems: https://github.com/metaphysis/Code.