August cookoff OBTTRNGL

what wrong with i logic:

              i calulate each equal angle  tp=360/k
             then b/w a and b h=tp*abs(b-a)
            if(h==ans) ans=0
           if(h<180) ans=abs(b-a-1)
           else ans=k-2-abs(b-a) plzz help....

here is my code:

Part I

Dividing loses precision. It may be very much precise, having an error in the order of ~ 10 ^ -12, but it won’t produce an exact result.

Instead you can do this.

omit this statement “tp = 360/k/1.;”

you know that tp * k == 360
therefore, h * k = 360 * (b - a)

  h = (360 * (b-a)) / k;
  if(h==180) ans=0;

Always use the precision loss statement in the final step. This way, you deal with exact figures.

Part II

We are not sure whether b is greater than a, therefore add the following statements:

tpa = a;
tpb = b;

a = min(tpa, tpb);
b = max(tpa, tpb);

You can find the solution link here.

1 Like

Try to avoid going into double/float as much as you can. “==” doesnt work with double as it works with int.

Heyy for second part i already used abs function…


Are you able to implement the first part?

If not, post the code link (in which you used abs function).