ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1294. Mars Satellites

whats in 3 test?? what s wrong?
Posted by ivan 3 Apr 2012 22:40
my code c#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace kyrsach1294
{
    class Program
    {
        static int Main(string[] args)
        { int a,b,c,d;
            string[] dlins = Console.ReadLine().Split(' ');
         a = int.Parse(dlins[0]);
         b = int.Parse(dlins[1]);
         c = int.Parse(dlins[2]);
         d = int.Parse(dlins[3]);
         if (a * b==c * d)
         {
             {
                 Console.WriteLine("Impossible.");
                 return 0;
             }
         }


       double s,s1,s2;
       s1 = c * d / (c * d - a * b);

        s2 = a * b / (c * d - a * b);
       s = s1*a*a+s1*b*b-s2*c*c-s2*d*d;
            if (s < 0)
            {
                Console.WriteLine("Impossible.");
                return 0;
            }
            int answer;

            s = Math.Sqrt(s) * 1000;
            answer = Convert.ToInt32(s);
            Console.WriteLine("Distance is "+answer+" km.");
            return 0;
        }
    }
}
please help

Edited by author 03.04.2012 22:40
Re: whats in 3 test?? what s wrong?
Posted by ZamNick 13 Nov 2012 03:22
Hi, guy.
Don't use Convert.ToInt32 for answer, it's wrong. You should go some other way. For example, first, I used double instead of int, and a small value EPS = 1e-9.
if(Math.Abs(a*b - c*d) < EPS)
{
     Console.WriteLine("Impossible.");
     return;
}

Second, I don't used :
if (s < 0)
{
     Console.WriteLine("Impossible.");
     return 0;
}
It's not necessary. I passed without this.

Third, you should approximate on another way. Convert.ToInt32 don't work correct, but I found the simple trick :
bool flag = false;
x = Math.Sqrt((a*b*(c*c + d*d) - c*d*(a*a + b*b)) / (a*b - c*d));
int y = ((int)(x * 10000.0)) % 10;
if (y >= 5) flag = true;
int ans = (int)(x * 1000.0);
if (flag == true) ++ans;
Console.WriteLine("Distance is {0} km.", ans);

I hope that I helped you. If you will get WA again and again I can send you correct code on your email.

4615234    03:01:21
13 ноя 2012    ZamNick    1294. Марсианские спутники    C#    Accepted
0.109    2 096 КБ