Tuesday, June 15, 2010

Tree Recovery

The Problem

Given the preorder and inorder traversal of a tree write the postorder traversal of the tree.


using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


using System.Collections;


namespace ConsoleApplication1


{


    class Program


    {


        static string answer = "";


        static void Main(string[] args)


        {


            string preord = "12345678";


            string inord = "32541876";


            former(preord, inord, false);


            while(s.Count>0)


            {


                answer = answer + s.Pop();


            }


            Console.WriteLine(answer);


        }


        static Stack s = new Stack();


        static void former(string preord, string inord, bool control)       


        {


            if (inord.Length == 1 || inord.Length < 1)


            {


                if (inord.Length == 1)


                {


                    answer = answer + inord;


                    if (control == true)


                    {


                        answer = answer + s.Pop();


                    }


                    return;


                }


                else


                {


                    if (control == true)


                    {


                        answer = answer + s.Pop();


                    }


                    return;


                }


            }


            else


            {


                int i=0, j=0;


                for (i = 0; i < preord.Length; i++)


                {


                    bool flag = false;


                    for (j = 0; j < inord.Length; j++)


                    {


                        if (preord[i] == inord[j])


                        {


                            flag = true;


                            break;


                        }


                    }


                    if(flag == true)


                    {break;}


                }


                string s3 = inord;


                string s4 = preord;


                //Left sub-tree in the inorder traversal.


                string s2 = inord.Remove(j, inord.Length - j);


                //Left sub-tree in the preorder traversal.


                string s1 = preord.Substring(i + 1, s2.Length);


                //Right sub-tree in the inorder traversal.


                string s5 = s3.Remove(0, j + 1);


                int index = s4.IndexOf(s1, 0);


                //Right sub-tree in the preorder traversal.


                string s6 = s4.Remove(index, s1.Length);


                //Push the root into the stack.


                s.Push(s6[0]);


                //Right sub-tree without the root in the preorder traversal.


                s6 = s6.Remove(0, 1);


                control = false;


                former(s1, s2, control);


                control = true;


                former(s6, s5, control);


            }


        }


    }

}

Sunday, June 13, 2010

Unidirectional Traveling Salesman Problem

using System;


using System.Collections.Generic;


using System.Text;


using System.Collections;


 


namespace ConsoleApplication1


{


    class Program


    {


        public static int minsum = 0, count = 0;


        public static int[] pathrow = new int[100];


        public static int[] pathcol = new int[100];


        public static int[] finalpathrow = new int[100];


        public static int[] finalpathcol = new int[100];


        static void Main(string[] args)


        {


            int[,] arr ={


                          {1,2,9},


                          {4,5,6},


                          {7,8,-1}


                        };


            fun(arr, 0, 0, 0, 0);


            for (int i = 0; i < arr.GetLength(1); i++)


            {


                Console.Write(arr[finalpathrow[i], finalpathcol[i]]);


                Console.Write(" ");


            }


            Console.WriteLine();


            Console.WriteLine(minsum);


        }


        public static void fun(int[,]arr, int row, int col, int thissum, int ele)


        {


            if(col==arr.GetLength(1))


            {


                if (count == 0)


                {


                    minsum = thissum;


                    for (int i = 0; i < arr.GetLength(1); i++)


                    {


                        finalpathrow[i] = pathrow[i];


                        finalpathcol[i] = pathcol[i];


                    }


                    count++;


                }


                else if (thissum < minsum)


                {


                    minsum = thissum;


                    for (int i = 0; i < arr.GetLength(1); i++)


                    {


                        finalpathrow[i] = pathrow[i];


                        finalpathcol[i] = pathcol[i];


                    }


                }


                return;


            }


            else if (row == -1 || row == arr.GetLength(0))


            {


                if (row == -1)


                {


                    row = arr.GetLength(0) - 1;


                    thissum = thissum + arr[row, col];


                    pathrow[ele] = row;


                    pathcol[ele] = col;


                    fun(arr, row + 1, col + 1, thissum, ele + 1);


                    fun(arr, row, col + 1, thissum, ele + 1);


                    fun(arr, row - 1, col + 1, thissum, ele + 1);


                }


                else


                {


                    row = 0;


                    thissum = thissum + arr[row, col];


                    pathrow[ele] = row;


                    pathcol[ele] = col;


                    fun(arr, row + 1, col + 1, thissum, ele + 1);


                    fun(arr, row, col + 1, thissum, ele + 1);


                    fun(arr, row - 1, col + 1, thissum, ele + 1);


                }


            }


            else


            {


                thissum = thissum + arr[row,col];


                pathrow[ele] = row;


                pathcol[ele] = col;


                fun(arr, row + 1, col + 1, thissum, ele + 1);


                fun(arr, row, col + 1, thissum, ele + 1);


                fun(arr, row - 1, col + 1, thissum, ele + 1);


            }


        }


    }

}

Saturday, June 12, 2010

Second Smallest and Second Largest in an array


using System;


using System.Collections.Generic;


using System.Text;


using System.Collections;


 


namespace ConsoleApplication1


{


    class Program


    {


        static void Main(string[] args)


        {


            int[] arr = {1,2,3,4,5,6,7,8,9,10};


            int smallest, secsmallest, largest, seclargest;


            //Take the first two elements as largest and secondlargest according to the following conditions.


            if (arr[0] < arr[1])


            {


                smallest = arr[0];


                secsmallest = arr[1];


                largest = arr[1];


                seclargest = arr[0];


            }


            else


            {


                smallest = arr[1];


                secsmallest = arr[0];


                largest = arr[0];


                seclargest = arr[1];


            }


            for (int i = 2; i < arr.Length; i++)


            {


                if (arr[i] < smallest || arr[i] > largest)


                {


                    //Element is smaller than smallest, smallest becomes secondsmallest.


                    if (arr[i] < smallest)


                    {


                        secsmallest = smallest;


                        smallest = arr[i];


                    }


                    //Element is larger than largest, largest becomes secondlargest.


                    else


                    {


                        seclargest = largest;


                        largest = arr[i];


                    }


                }


                else if ((arr[i]>smallest && arr[i]<secsmallest) ||     


                         (arr[i]<largest && arr[i]>seclargest))


                {


                    //Element lies between the largest and secondlargest, secondlargest assigned element.


                    if (arr[i] > smallest && arr[i] < secsmallest)


                    { secsmallest = arr[i]; }


                    else


                    { seclargest = arr[i]; }


                }


            }


            Console.WriteLine("Second smallest: " + secsmallest);


            Console.WriteLine("Second largst: " + seclargest);


        }


    }

}

Wednesday, June 9, 2010

Factorial of large numbers


using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


 


namespace ConsoleApplication1


{


    class Program


    {


        static void Main(string[] args)


        {


            string s1 = "30";


            int num1 = int.Parse(s1);


            int num2 = num1 - 1;


            //string s2 = num2.ToString();


            string ans = multiply(s1, num2);


            while (num2 > 1)


            {


                num2--;


                //s2 = num2.ToString();


                ans = multiply(ans, num2);


            }


            Console.WriteLine(ans);


        }


        static string multiply(string s1, int s2)


        {


            int length1 = s1.Length;


            int[] arr = new int[length1];


            for (int i = 0; i < length1; i++)


            {


                string str = "";


                str = str + s1[i];


                arr[i] = int.Parse(str);


            }


            int carry = 0;


            string answer = "";


            //Multiplying the second number(full) to the first number(digitwise).


            for (int i = length1-1; i >= 0; i--)


            {


                int ans = arr[i] * s2 + carry;


                int dig = ans % 10;


                carry = ans / 10;


                answer = answer + dig.ToString();


            }


            //Prefixing the remaining carry to the answer.


            if (carry != 0)


            {


                while (carry > 0)


                {


                    int m = carry % 10;


                    answer = answer + m.ToString();


                    carry = carry / 10;


                }


            }


            string Finalanswer = "";


            //Reversing the answer string to get the Finalanswer.


            for (int i = answer.Length - 1; i >= 0; i--)


            {


                Finalanswer = Finalanswer + answer[i];


            }


            return Finalanswer;


        }


    }

}

Tuesday, June 8, 2010

Triangle Wave


In this problem you are to generate a triangular wave form according to a specified pair of Amplitude and Frequency.


Input and Output


The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.


Each input set will contain two integers, each on a separate line. The first integer is the Amplitude; the second integer is the Frequency.


For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.


For the output of your program, you will be printing wave forms each separated by a blank line. The total number of wave forms equals the Frequency, and the horizontal ``height'' of each wave equals the Amplitude. The Amplitude will never be greater than nine.


The waveform itself should be filled with integers on each line which indicate the ``height'' of that line.


NOTE: There is a blank line after each separate waveform, excluding the last one.


Sample Input

1 
3
2

Sample Output

1
22
333
22
1
 
1
22
333
22
1

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Scores

{

    class Program

    {

        static void Main(string[] args)

        {

            string s1 = Console.ReadLine();

            string s2 = Console.ReadLine();

            int amp = int.Parse(s1);

            int freq = int.Parse(s2);

            int j = 0;

            for (int i = 0; i < freq; i++)

            {

                //For increasing side.

                while (j < amp)

                {

                    j++;

                    for (int k = 0; k < j; k++)

                    {

                        Console.Write(j);

                    }

                    Console.WriteLine();

                }

                //For decresing side.

                while (j > 0)

                {

                    j--;

                    for (int k = 0; k < j; k++)

                    {

                        Console.Write(j);

                    }

                    Console.WriteLine();

                }

            }

        }

    }

}

The Snail


A snail is at the bottom of a 6-foot well and wants to climb to the top. The snail can climb 3 feet while the sun is up, but slides down 1 foot at night while sleeping. The snail has a fatigue factor of 10%, which means that on each successive day the snail climbs 10% 3 = 0.3 feet less than it did the previous day. (The distance lost to fatigue is always 10% of the first day's climbing distance.) On what day does the snail leave the well, i.e., what is the first day during which the snail's height exceeds 6 feet? (A day consists of a period of sunlight followed by a period of darkness.) As you can see from the following table, the snail leaves the well during the third day.





























Day


Initial Height


Distance Climbed


Height After Climbing


Height After Sliding


1


0'


3'


3'


2'


2


2'


2.7'


4.7'


3.7'


3


3.7'


2.4'


6.1'


-


Your job is to solve this problem in general. Depending on the parameters of the problem, the snail will eventually either leave the well or slide back to the bottom of the well. (In other words, the snail's height will exceed the height of the well or become negative.) You must find out which happens first and on what day.


Input 


The input file contains one or more test cases, each on a line by itself. Each line contains four integers H, U, D, and F, separated by a single space. If H = 0 it signals the end of the input; otherwise, all four numbers will be between 1 and 100, inclusive. H is the height of the well in feet, U is the distance in feet that the snail can climb during the day, D is the distance in feet that the snail slides down during the night, and F is the fatigue factor expressed as a percentage. The snail never climbs a negative distance. If the fatigue factor drops the snail's climbing distance below zero, the snail does not climb at all that day. Regardless of how far the snail climbed, it always slides D feet at night.


Output 



For each test case, output a line indicating whether the snail succeeded (left the well) or failed (slid back to the bottom) and on what day. Format the output exactly as shown in the example.


Sample Input 


6 3 1 10


10 2 1 50


50 5 3 14


50 6 4 1


50 6 3 1


1 1 1 1


0 0 0 0


Sample Output 


success on day 3


failure on day 4


failure on day 7


failure on day 68


success on day 20


failure on day 2


using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


 


namespace Scores


{


    class Program


    {


        static void Main(string[] args)


        {


            string s = Console.ReadLine();


            string s1 = "";


            float[] A = new float[10];


            int j = 0;


            for (int i = 0; i < s.Length; i++)


            {


                s1 = s1 + s[i];


                if (s[i] == ' ' || i==s.Length-1)


                {


                    A[j] = float.Parse(s1);


                    j++;


                    s1 = "";


                }


            }


            float h = 0;


            int count = 0;


            while (h >= 0 && h < A[0])


            {


                if (count == 0)


                {


                    h = h + A[1];


                    count++;


                    if (h > A[0])


                    {


                        break;


                    }


                    A[4] = h;


                    h = h - A[2];


                    if (h < 0)


                    {


                        break;


                    }


                }


                else


                {


                    A[4] = A[4] - ((A[3] / 100.0f) * A[1]);


                    h = h + A[4];


                    count++;


                    if (h > A[0])


                    {


                        break;


                    }


                    h = h - A[2];


                    if (h < 0)


                    {


                        break;


                    }


                }


            }


            if (h < 0)


            {


                Console.WriteLine("Failure on day " + count);


            }


            else if (h >= 0)


            {


                Console.WriteLine("Success on day " + count);


            }


        }


    }


}