Sunday, January 9, 2011

To print input till output is 42 !

This is a just a problem I found in CodeChef (Thanks a ton to Bittu for introducing this site to me :-)) :-)


Problem :


Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.


Code I have tried :



using System;


namespace LifeTheUniverse
{
    class ClsCodeChef1
    {
        static void Main()
        {
            int a = 0, i = 0;
            int[] b = new int[1];
            Console.WriteLine("Input:");
            while (a != 42)
            {
                a = Convert.ToInt32(Console.ReadLine());
                b[i] = a;
                i++;
                Array.Resize(ref b, i + 1);
            }
            Console.WriteLine("Output:");
            for (int j = 0; j < i - 1; j++)
                Console.WriteLine(b[j]);
            Console.Read();
        }
    }
}

No comments:

Post a Comment