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 1140. Swamp Incident

AC on C but Runtime error on C#
Posted by Semm 16 Feb 2015 19:47
Hi, I've solved the problem easily on C, but can't solve it on C# (almost the same code), getting Runtime Error on #2.
Can someone point out what's wrong in my code?

using System;

namespace Application
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] pos = new int[3];
            for(int i=0; i<n; i++)
            {
                string[] s = Console.ReadLine().Split(' ');
                int ind = s[0][0] - 'X';
                pos[ind] += int.Parse(s[1]);
            }

            while(true)
            {
                if(pos[0] * pos[2] > 0)
                {
                    int sign = pos[0] / Math.Abs(pos[0]);
                    int min = Math.Min(Math.Abs(pos[0]), Math.Abs(pos[2]));
                    pos[0] -= sign * min;
                    pos[1] += sign * min;
                    pos[2] -= sign * min;
                }
                else if(pos[0] * pos[1] < 0)
                {
                    int sign = pos[0] / Math.Abs(pos[0]);
                    int min = Math.Min(Math.Abs(pos[0]), Math.Abs(pos[1]));
                    pos[0] -= sign * min;
                    pos[1] += sign * min;
                    pos[2] -= sign * min;
                }
                else if(pos[1] * pos[2] < 0)
                {
                    int sign = pos[2] / Math.Abs(pos[2]);
                    int min = Math.Min(Math.Abs(pos[1]), Math.Abs(pos[2]));
                    pos[0] -= sign * min;
                    pos[1] += sign * min;
                    pos[2] -= sign * min;
                }
                else
                {
                    break;
                }
            }
            int nOfDir = 0;
            if(pos[0] != 0)
                nOfDir++;
            if(pos[1] != 0)
                nOfDir++;
            if(pos[2] != 0)
                nOfDir++;
            Console.WriteLine(nOfDir);
            if(pos[0] != 0)
                Console.WriteLine("X " + (-pos[0]).ToString());
            if(pos[1] != 0)
                Console.WriteLine("Y " + (-pos[1]).ToString());
            if(pos[2] != 0)
                Console.WriteLine("Z " + (-pos[2]).ToString());
        }
    }
}
Re: AC on C but Runtime error on C#
Posted by kasarino 30 Jul 2016 19:32
To get AC instead of Console.ReadLine() I used
Console.In.ReadToEnd().Split(new char[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)