Cable Master: I can't find my error, i think my program is Ok(+) Anyone help me :)

Here it is:

#include<iostream.h>

#include<stdio.h>

int n, k;

long x[10000];

void main()

{

double xx;

int i, j;

cin>>n>>k;

for (i=0; i<n; i++)

{

cin>>xx;

x[i] = xx*100;

}

long l, r, m, w;

r = x[0];

for (i=1; i<n; i++)

if (x[i]>r) r=x[i];

l = 1;

w = 0;

while ( l <= r )

{

m = (l + r) / 2;

j = 0;

for (i=0; i<n; i++)

j = j + (long)x[i]/m;

if ( j == k )

{

w = m;

}

if (j >= k)

l = m + 1;

else

r = m - 1;

}

double ans = (double)w/100.0;

printf("%.2f\n",ans);

}

Two mistake.

I'm not good at C++.

I like Dephi and C more than C++.

But I had correct your program...

Do you have E-mail?

I'll send it to you

Two mistakes

Well......

The first error is

x[i] = xx*100;

You should have replaced the line to

x[i]=(long)(xx*100+0.1);

Try the testcase

4 4

2.01

2.01

2.01

2.01

Your program output a wrong answer

2.00

:-)

The second one

Why do you think that the cable must be cut into exactly k pieces?

So you must change

if ( j == k )

{

w = m;

}

into

if (j>=k && w>m)

{w=m;}

Wow, how did you know the first mistake?? Lot of thanks :) (+)

Could you explain me why x[i] = (long)(x*100 + 0.1), how did you know

0.1??

Thanks :)

HeHe:)

We can try to this code.

double n=2.01;

long x;

x=(long)(x*100);

I was surprised that the result is 200?

I can't understood it also.

Then this code

double n=2.01,t;

long x;

t=n*100;

x=(long) t;

You can find out that

t=201.00000

but

x was still 201.

So I use (n*100+0.1) instead Then I use your program to got AC

By the way .

I like C more than C++ beacuse I don't kown C++ well

If I made any mistake in this problem please forgive me.:-)

Good luck.