Tuesday, June 8, 2010

Perfect Cubes

For hundreds of years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that an+bn+cn, has remained elusively unproven. (A recent proof is believed to be correct, though it is still undergoing scrutiny.) It is possible, however, to find integers greater than 1 that satisfy the ``perfect cube'' equation a3=b3+c3+d3 (e.g. a quick calculation will show that the equation 123=63+83+103 is indeed true). This problem requires that you write a program to find all sets of numbers {a, b, c, d} which satisfy this equation for a<=200.


Output


The output should be listed as shown below, one perfect cube per line, in non-decreasing order of a (i.e. the lines should be sorted by their a values). The values of b, c, and d should also be listed in non-decreasing order on the line itself. There do exist several values of a which can be produced from multiple distinct sets of b, c, and d triples. In these cases, the triples with the smaller b values should be listed first.


The first part of the output is shown here:


Cube = 6, Triple = (3,4,5)


Cube = 12, Triple = (6,8,10)


Cube = 18, Triple = (2,12,16)


Cube = 18, Triple = (9,12,15)


Cube = 19, Triple = (3,10,18)


Cube = 20, Triple = (7,14,17)


Cube = 24, Triple = (12,16,20)

 


using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


 


namespace Cubes


{


    class Program


    {


        static void Main(string[] args)


        {


            for (int i = 6; i < 200; i++)


            {


                for (int j = 2; j < 200; j++)


                {


                    for (int k = j + 1; k < 200; k++)


                    {


                        for (int l = k + 1; l < 200; l++)


                        {


                            if ((i*i*i) == (j*j*j) + (k*k*k) + (l*l*l))


                            {


                                Console.WriteLine(i +" "+ j +" "+ k +" "+ l);


                            }


                        }


                    }


                }


            }


        }


    }


}

Encoder and Decoder


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();


            if (!char.IsDigit(s[0]))//Encoder Section.


            {


                string s1 = "";


                string s2 = "";


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


                {


                    int ch = s[i];//Retreivimg the ASCII code of ith character.


                    s1 = s1 + ch.ToString();//COnctenating it into a string.


                }


                //Reversing the string of ASCII values.


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


                {


                    s2 = s2 + s1[i];


                }


                Console.WriteLine(s2);


            }


            else//Decoder Section.


            {


                string s1 = "";


                string s2 = "";


                //Reversing the input encoded string.


                for (int j = s.Length - 1; j >= 0; j--)


                {


                    s1 = s1 + s[j];


                }


                int i = 0;


                while (i < s1.Length)


                {


                    int count = 0;


                    string s3 = "";


                    while (count < 2)//Taking two digits at a time.


                    {


                        s3 = s3 + s1[i];


                        i++;


                        count++;


                    }


                    count = int.Parse(s3);


                    if (count < 32)//Condition for taking three digits at a time.


                    {


                        s3 = s3 + s1[i];


                        i++;


                        count = int.Parse(s3);


                    }


                    s2 = s2 + char.ConvertFromUtf32(count);//Concatenating the character to the string.


                }


                Console.WriteLine(s2);


            }


        }


    }


}

Monday, June 7, 2010

Inheritance in C#


using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


 


namespace Test_in_cs


{


    class publisher


    {


        String title;


        float price;


        public publisher(String t, float p)


        {


            title = t;


            price = p;


        }


        public void displaydata()


        {


            Console.WriteLine("Title: " + title);


            Console.WriteLine("Price: " + price);


        }


    }


    class book : publisher


    {


        int page;


        public book(String t, float p, int c)


            : base(t, p)//Syntax for declaring a derived class.


        {


            page = c;


        }


        public void display()


        {


            base.displaydata();


            Console.WriteLine("Total pages: " + page);


        }


    }


    class Program


    {


        static void Main(string[] args)


        {


            book b = new book("Let us C#", 180.0f, 475);


            b.display();


        }


    }


}

Static members in C#


using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


 


namespace Test_in_cs


{


    //Class for demonstration of static member.


    class staticdemo


    {


        private static int count;


        private int info;


        public staticdemo()//Constructor.


        {


            count++;//Static member increments with every instantiation.


        }


        public static void num()//Static method.


        {


            Console.WriteLine("Number of objects is: " + count);


        }


        public int setter(int a)


        {


            info = a;


            return info;


        }


    }


    class Program


    {


        static void Main(string[] args)


        {


            Console.Write("Enter the number of objects to be created: ");


            String s = Console.ReadLine();


            int n = int.Parse(s);


            staticdemo[] obj = new staticdemo[n];//Only memory allocation.


            staticdemo.num();


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


            {


                obj[i] = new staticdemo();//Actual instantiation.


                Console.Write("Information in object " + (i+1));


                Console.WriteLine("is " + obj[i].setter(i + 1));


            }


            staticdemo.num();


        }


    }


}

Saturday, June 5, 2010

Classes and Objects in C#


using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


 


namespace Test_in_cs


{


    class cone


    {


        private float height, radius;


        public cone(float h, float r)


        {


            height = h;


            radius = r;


        }


        public void setdata(float h, float r)


        {


            height = h;


            radius = r;


        }


        public void displaydata()


        {


            Console.WriteLine("Height= " + height);


            Console.WriteLine("Radius= " + radius);


        }


        public void volume()


        {


            float v;


            v = (1 / 3.0f) * 3.14f * radius * radius * height;


            Console.WriteLine("Volume= " + v);


        }


    }


    class Program


    {


        static void Main(string[] args)


        {


            //Class instantiation and member calling.


            cone c1 = new cone(10.0f, 3.5f);


            cone c2 = new cone(20.0f, 6.2f);


            c1.displaydata();


            c1.volume();


            c2.displaydata();


            c2.volume();


            Console.WriteLine("\nData after setting:");


            c1.setdata(100.0f,100.0f);


            c1.displaydata();


            //Class instantiation and member calling.


            Console.Write("Enter the string: ");


            String s2 = Console.ReadLine();


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


            {


                if (s2[i] == ' ')


                {


                    s2 = s2.Remove(i, 1);


                    s2 = s2.Insert(i,"space");


                }


            }


            Console.WriteLine(s2);


        }


    }


}

Tuesday, June 1, 2010

Longest Common Subsequence


Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences:


abcdgh


aedfhr


is adh of length 3.


Input consists of pairs of lines. The first line of a pair contains the first string and the second line contains the second string. Each string is on a separate line and consists of at most 1,000 characters. 


For each subsequent pair of input lines, output a line containing one integer number which satisfies the criteria stated above.


 


Sample input


a1b2c3d4e


zz1yy2xx3ww4vv


abcdgh


aedfhr


abcdefghijklmnopqrstuvwxyz


a0b0c0d0e0f0g0h0i0j0k0l0m0n0o0p0q0r0s0t0u0v0w0x0y0z0


abcdefghijklmnzyxwvutsrqpo


opqrstuvwxyzabcdefghijklmn


 


Output for the sample input


4


3


26


14


#include "stdafx.h"


#include "string.h"


 


int _tmain(int argc, _TCHAR* argv[])


{


      int lena,lenb,i,j,k,ind,maxlen,templen,found;


      char A[100],B[100];


      while(scanf("%s",A))


      {


            maxlen=0;templen=0;found=0;


            scanf("%s",B);


            lena=strlen(A);


            lenb=strlen(B);


            for(i=0;i<lena;i++)


            {


                  ind=-1;j=i;templen=0;


                  while(j<lena)


                  {


                        found=0;


                        for(k=ind+1;k<lenb;k++)


                        {


                              if(A[j]==B[k] && j<lena)


                              {


                                    templen++;


                                    ind=k;


                                    j++;


                                    found=1;


                                    break;


                              }


                        }


                        if(found!=1)


                        {


                              j++;


                        }


                  }


                  if(maxlen<templen)


                  {


                        maxlen=templen;


                  }


            }


            printf("%d\n",maxlen);


      }


      return 0;


}

Wednesday, February 10, 2010

Big Mod




Calculate
R:=BP
mod M, meaning remainder R when B raised to the power of P is divided by M.

for large values of B, P, and M using an efficient algorithm.
(That's right, this problem has a time dependency !!!.)


Input



Three integer values (in the order B, P, M) will be read one number per line. B and P are integers in the range 0 to 2147483647 inclusive. M is an integer in the range 1 to 46340 inclusive.


Output



The result of the computation. A single integer.


Sample Input



3
18132
17

17
1765
3

2374859
3029382
36123


Sample Output



13
2
13195

// Big-Mod.cpp : Defines the entry point
for the console application.



//



 







#include "stdafx.h"



int _tmain(int argc, _TCHAR* argv[])
{
                void
bigmod(long int x,long int y,long int z);
                long int B,P,M;
                while(cin>>B)
                {
                                cin>>P;
                                cin>>M;
                                bigmod(B,P,M);
                }
                return 0;
}
void bigmod(long int x,long int y,long int z)
{
                int pow,rem;
                rem=x%z;
                for(pow=1;pow<y;pow++)
                {
                                rem=(rem*(x%z))%z;
                }
                cout<<rem<<endl;
}