Square Root Problem 18

Back to General discussions forum

jury16     2018-09-20 17:35:12

C# Hi! i don't know where is my mistake. with this cod i have the results more precise than in the exercises, but the test says to me i am wrong. Can someone help me to describe my mistake?

My cod:

   class Program
     {
      static void Main(string[] args)
      {
        StringBuilder answer = new StringBuilder();
        int n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            string[] line = Console.ReadLine().Split(' ');
            int[] arr = line.Select(x => int.Parse(x)).ToArray();
            int numberSteps = arr[1];
            int number = arr[0];
            double  r = 1;
            for (int j = 0; j < numberSteps; j++)
            {
                 r = (r + number / r) / 2;
                 r = Math.Round(r, 12);
            }
            answer.Append(r);
            answer.Append(" ");
        }
        Console.WriteLine(answer);
        Console.WriteLine();
       }
    }

My answers: 8,732993701398 152,361855184417 33,5 28,12472222085 8,246211251236 7592,49982708812 4097,5 38,392831314275 4,126106627581 155,735313093754 134,884803684064 209,442801425437

Test answers: 8.7329937014 152.361855184 33.5 28.1247222209 8.24621125124 7592.49982709 4097.5 38.3928313143 4.12610662758 155.735313094 134.884803684 209.442801425

Data: 12 70 4 23214 10 66 1 791 13 68 7 60719 3 8194 1 1474 8 17 4 24121 9 3981 5 34139 8

*Results should have precision of 1e-7 = 0.0000001 or better!

Thank you!

Quandray     2018-09-20 20:16:42
User avatar

Your answers contain commas, when decimal points are expected.

jury16     2018-09-21 03:11:31

thank you friend! :)

*I added the line:

 System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

and everything OK!

Kalamona     2020-02-11 08:13:27

Expected answer was: 26.0768096208 59.5250365606 249.442676272 68.9855550187 6208.49995973 80.4611707596 130.859487866 8.88819441732 155.93008754 948.933892662 245.498977505

My answer: 26.076809620810593 29.478805945967352 242.2643184622944 68.98550572402873 165.11514544971604 80.46117075956576 84.02380615040002 8.888194417315589 155.92947123619706 243.6657546722559 65.86351647217765

data: 11 680 10 869 4 58692 9 4759 9 24829 2 6474 12 7060 6 79 10 24314 10 59373 6 977 2

program(python): r = 1 n = int(input()) for i in range(n): v,x = map(int, input().split()) for j in range(x): d = v/r r =(r+d)/2 print(r)

Manually calculating my results is good, I think expected answer is wrong, or my code's wrong?
Some opinion?

Rodion (admin)     2020-02-11 09:36:44
User avatar

Hi Friend!

I think expected answer is wrong,

well, see :) this problem was solved by over 3500 users. if there were bugs in the checker, we most probably have already found them all :)

or my code's wrong?

yes, it is not obvious, but your code really has funny small mistake. I can tell you, but I believe you can find it yourself with debugging and it will be more useful for you. Try adding line

print('J,D,R: ', j, d, r)

as the last line of loop, so you can see values at every iteration. You have error with the second-test case. Try verifying it manually.

I'm sure you'll see at once. Feel free to ask more hints if needed :)

Kalamona     2020-02-13 08:15:52

Funny. :) My r=1 is in the wrong place, thnx.

Please login and solve 5 problems to be able to post at forum