Sunday, January 24, 2010

Snowfall

J&K is suffering from a heavily
snowy winter.The heavy snow even causes the closure of an important highway
connecting J&K and rest of India. You've got several reports containing the
start and end points of highway segments covered by heavy snow. Given those
reports , you are to return the total length of highway segments covered by
snow. Note that the reported segments may overlap.



Input



Input consists of T(T<=100)
number of test cases. T cases follow. Each input case starts with a number N ,
the number of road segments covered by snow. Next line contains N numbers , the
start points of the segment. Next line contains N numbers , the end points of
the segment. The points will always be <= 10000. For any particular segment
start point will always be less than the end point.



Output



For each test case print case number
followed by the total length of highway segments covered by snow. Suppose ur
output for 1st test case is 25 , then print "Case 1:25".



Sample Input















2
3
17
85 57
33
86 84
8
45
100 125 10 15 35 30 9
46
200 175 20 25 45 40 10



Sample Output





Case
1:44
Case
2:132















































































































































//  Snowfall.cpp : Defines the entry point for the console application.


//


 


#include "stdafx.h"


 


 


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


int main()
{
                int
T,N,A[100][100],temp1,temp2,i,j,test,dist,s,e;
                cin>>T;  
       
        test=0;
                while(test<T)
                {
                                dist=0;
                                cin>>N;
                                for(i=0;i<2;i++)
                               {
                                                for(j=0;j<N;j++)
                       
                        {
                                                                cin>>A[i][j];
                      
                        }
                                }
                                for(i=0;i<N;i++)
                                {
                                                for(j=0;j<=N-i;j++)
                       
                        {
                                                                if(A[0][j]>A[0][j+1]
&& (j+1)<N)
                                                                {
                                                                                temp1=A[0][j];
                                                                                A[0][j]=A[0][j+1];                          
          
                                                                     A[0][j+1]=temp1;
                                                                                temp2=A[1][j];
                                                                                A[1][j]=A[1][j+1];
                                                                                A[1][j+1]=temp2;
                                                                }
                       
                        }
                                }
                                /*for(i=0;i<2;i++)
                                {
                                                for(j=0;j<N;j++)
                       
                        {
                            
                                    printf("
%d",A[i][j]);
                       
                        }
                                                printf("\n");
                                }*/
                                s=A[0][0];e=A[1][0];dist=0;i=0;
                                for(j=1;j<N;j++)
                                {
                                                if(A[i][j]>e)
                       
                        {
                             
                                  dist=dist+(e-s);
                                                                s=A[i][j];
                                                                e=A[i+1][j];
                       
                        }
                       
                        else
if(A[i][j]<e && A[i+1][j]<e)
                       
                        {
                           
                                    if(j!=N-1)
                       
                                        {continue;}
                       
                        }
                       
                        else
if(A[i][j]<=e && A[i+1][j]>e)
                       
                        {
                          
                                     e=A[i+1][j];
                       
                        }
                       
                        if(j==N-1)
                       
                        {
                           
                                    dist=dist+(e-s);
                       
                        }
                                }                                                                                                                                        
 
                               cout<<"Case
"<<test+1<<":"<<dist<<endl;
                                test++;
                }
                //scanf("%d",&i);
                return 0;
}

2
3
17 85 57
33 86 84
8
45 100 125 10 15 35 30 9
46 200 175 20 25 45 40 10

Case 1:44
Case 2:132                                         




How to feed input from a text file into a CPP File without using File Handling

1. Initial syntax in the program should be to  read a data into a variable repeatedly.
    Example:
    int main()
    {
                int A;
                while(cin>>A)
                {
                            .
                            .
                            .
                }
                return 0;
    }
2. Create an input text file containing the input in the prescribed format.
    Example: Say the input text file is named input.
3. Save the text file in a directory.
    Example: Documents.
4. Open command prompt.
5. Reach the directory using suitable commands.
    Example:
    C:\Users\Sourabh-PC>cd Documents
6. Now the command is Filename.exe<input.txt>output.txt
    Example:
    C:\Users\Sourabh-PC\Documents>Snowfall.exe<input.txt>output.txt
7. This creates a text file named output in the same directory containing the output.

Monday, January 18, 2010

All in All

You have devised a new encryption technique which
encodes a message by inserting between its characters randomly generated
strings in a clever way. Because of pending patent issues we will not discuss
in detail how the strings are generated and inserted into the original message.
To validate your method, however, it is necessary to write a program that
checks if the message is really encoded in the final string.



Given two strings s and t, you have
to decide whether s is a subsequence of t, i.e. if you can remove
characters from t such that the concatenation of the remaining
characters is s.



Input Specification



The input contains several test cases. Each is
specified by two strings s, t of alphanumeric ASCII characters separated
by whitespace. Input is terminated by EOF.



Output Specification



For each test case output, if s is a
subsequence of t.



Sample Input



sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter


Sample
Output



Yes
No
Yes
No








































































//  All in All.cpp : Defines the entry point for the console application.


//


 


#include "stdafx.h"


 


 


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


{
                char
A[100],B[100];
                int pos,tru,found=0,i,j;
                while(scanf("%s",A))
                {
                                pos=0;tru=0;
                                scanf("%s",B);
                                for(i=0;A[i]!='';i++)
                                {
                                                tru=0;
                       
                        for(j=pos;B[j]!='';j++)
                       
                        {
                        
                                       if(A[i]==B[j])
                                                                {
                                                                                tru=1;
                                                                                pos=j;
                                                                                break;
                                                                }
                      
                         }                                                                                   
                       
                        if(tru==0)
                       
                        {
                                                                found=0;
                                                                break;
                       
                        }
                       
                        else
                       
                        {found=1;}
                                }
                                if(found==1)
                                {printf("Yes\n");}
                                else
                                {printf("No\n");}
                }
                return 0;
}

sequence subsequence
Yes
person compression
No
VERDI vivaVittorioEmanueleReDiItalia
Yes
caseDoesMatter CaseDoesMatter
No

Saturday, January 16, 2010

Floor Tiles



The Problem



Given an arrangement of tiles in terms of the characters ‘-‘and
‘|’, you have to find the minimum number of tiles required to cover the whole
floor under the following constraints: in a row (i,j) if there is character ‘-‘
in adjacent columns(j’s) of the same row they are the part of the same tile.
Similarly if there is character ‘|’ in adjacent rows (i’s) of the same column
they are the part of the same tile. Same character in different rows or columns
counts a different tile. (See the examples below).



The input



The first line of input will contain two integers m,n separated
by white spaces representing the dimensions of the floor. Next m lines will
contain n characters each representing the arrangement of the tiles.



The output



For each input output the minimum number of tiles required
to cover the floor in a new line.



Sample input





















4 4
----
----
----
----

4 4
-|--
-|--
||--
||--



Sample output



4
8

//  Floor Tiles.cpp : Defines the entry point for the console application.


//


 


#include "stdafx.h"


 


 


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


{
                char
A[50][50];
                int m,n,i=0,j=0,count=0,r,c;
                while(cin>>m)
                {
                                cin>>n;
                                count=0;
                                for(i=0;i<m;i++)
                                {
                                                cin>>A[i];
                                }
                                for(i=0;i<m;i++)
                                {
                                                r=0;
                       
                        for(j=0;j<n;j++)
                       
                        {
                          
                                     if(A[i][j]=='-' && r==0)
                                                                {
                                                                                count++;
                                                                                r=1;
                                                                }
                                                                else
if(A[i][j]!='-')
                                                                {r=0;}
                       
                        }
                                }
                                for(i=0;i<n;i++)
                                {
                                                c=0;
                      
                         for(j=0;j<m;j++)
                       
                        {
                         
                                      if(A[j][i]=='|' && c==0)
                                                                {
                                                                                count++;
                                                                                c=1;
                                                                }
                                                                else
if(A[j][i]!='|')
                                                                {c=0;}
                      
                         }
                                }
                                cout<<count<<"\n";
                }
                return 0;
}

4 4
----
----
----
----
4

4 4
-|--
-|--
||--
||--
8

5 5
-|-|-
|-|-|
-|-|-
|-|-|
-|-|-
25

Wednesday, January 13, 2010

Bicoloring



The Problem

In 1976 the ``Four Color Map Theorem" was proven with the assistance of a computer. This theorem states that every map can be colored using only four colors, in such a way that no region is colored using the same color as a neighbor region.


Here you are asked to solve a simpler similar problem. You have to decide whether a given arbitrary connected graph can be bicolored.
That is, if one can assign colors (from a palette of two) to the nodes in such a way that no two adjacent nodes have the same color. To simplify the problem you can assume:


  • no node will have an edge to itself.
  • the graph is nondirected. That is, if a node a is said to be connected to a node b, then you must assume that b is connected to a.
  • the graph will be strongly connected. That is, there will be at least one path from any node to any other node.


Input
 


The input consists of several test cases. Each test case starts with a line containing the number n (1 < n < 200) of different nodes. The second line contains the number of edges l. After this, l lines will follow, each containing two numbers that specify an edge between the two nodes that they represent. A node in the graph will be labeled using a numbera .

An input with n = 0 will mark the end of the input and is not to be processed.


Output
 


You have to decide whether the input graph can be bicolored or not, and print it as shown below.


Sample Input 


3
3
0 1
1 2
2 0
9
8
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0

Sample Output


NOT BICOLORABLE.
BICOLORABLE.


//  Bicoloring.cpp : Defines the entry point for the console application.


//


 


#include "stdafx.h"


 


 


//  Bicoloring.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

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

{
    while(scanf("%d",&n))
 {
  first=1;
        if(n!=0)
        {
            tru=0;
            scanf("%d",&e);
   for(i=0;i<n;i++)
   {
    B[i]=3;
   }
   for(i=0;i<e;i++)
     
   {
    for(j=0;j<2;j++)
    {
     scanf("%d",&A[i][j]);
     if(first==1 && B[A[i][j]]==3)
     {
      B[A[i][j]]=1;
      first=2;
     }
     else if(B[A[i][j]]==3)
     {
      if(j>0 && B[A[i][j-1]]==1)
      {
       B[A[i][j]]=2;
      }
      else if(j>0 && B[A[i][j-1]]==2)
      {
       B[A[i][j]]=1;
      }
      else if(j==0 && B[A[i][j+1]]==1)
      {
       B[A[i][j]]=2;
      }
      else if(j==0 && B[A[i][j+1]]==2)
      {
       B[A[i][j]]=1;
      }
     }        
    }
   }  
   for(i=0;i<e;i++)
   {
    for(j=0;j<2;j++)
    {
     col[j]=B[A[i][j]];
    }
    if(col[0]==col[1])
    {
     tru=1;
     break;
    }
   }
   if(tru==1)
   {printf("Not bicolorable.\n");}
   else
   {printf("Bicolorable.\n");}
  }
  else
  {return 0;}
 }
 return 0;
}

Output

3
3
0 1
1 2
2 0
Not bicolorable.

9
8
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
Bicolorable.



Monday, January 11, 2010

Error Correction

// Error Correction.cpp : Defines the entry point for the console application.


//


 


#include "stdafx.h"


 


 


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


{
                int
n,i=0,j=0,A[100][100],sum1=0,sum2=0,rc,cc,tru=0,rd,cd;
                while(scanf("%d",&n))
                {
                                if(n!=0)
                                {
                                                rc=0;cc=0;tru=0;
                       
                        for(i=0;i<n;i++)
                       
                        {
                         
                                      for(j=0;j<n;j++)
                                                                {
                                                                                scanf("%d",&A[i][j]);
                                                                }
                                                }
                      
                         for(i=0;i<n;i++)
                       
                        {
                           
                                    sum1=0;
                                                                sum2=0;
                                                                for(j=0;j<n;j++)
                                                                {
                       
                                                        sum1=sum1+A[i][j];
                                                                                sum2=sum2+A[j][i];
                                                                }
                                                                if(sum1%2!=0
|| sum2%2!=0)
                                                                {
                                                                                if(sum1%2!=0)
                                                                                {
                                                                                                rc++;
                                               
                                                rd=i;
                                                                                }
                                                                                else
if(sum2%2!=0)
                                                                                {
                                                                                                cc++;
                                               
                                                cd=i;
                                                                                }
                                                                                if(rc>1
|| cc>1)
                                                                                {
                                                                                                tru=1;
                                               
                                                break;
                                                                                }                                           



































                          
                                     }
                      
                         }
                       
                        if(rc==0
&& cc==0)
                       
                        {printf("OK\n");}
                       
                        else
if(tru==1)
                       
                        {printf("Corrupt\n");}
                       
                        else
if(rc!=cc)
                       
                        {printf("Corrupt\n");}
                       
                        else
if(rc==1 && cc==1)
                      
                         {
                         
                                      printf("Change bit
(%d,%d)\n",rd+1,cd+1);
                                                }           
                               
}
                                else
                                {return 0;}
                }
}



4
1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1
OK

4
1 0 1 0
0 0 1 0
1 1 1 1
0 1 0 1
Change bit (2,3)

4
1 0 1 0
0 1 1 0
1 1 1 1
0 1 0 1
Corrupt

Sunday, January 10, 2010

Permute function in draw the truth table program

void permute(int n)
{
                int
in=10,i=0,A[100],j,a,b,t,B[100],m,k,l,test,move=0,num=0,c,d=0,top=-1,value1,value2,value,opns[100];
                printf("The
truth table is\n\n");
                for(j=0;os[j]!='';j++)
                {
                                if(isalpha(os[j])!=0)
                                {printf("%c ",os[j]);}
                }
                printf("Y\n");
                while(in>0)//Resolving
the number into digits.
                {
                                A[i]=in%10;
                                in=in/10;
                                i++;
                }
                for(a=0;a<i;a++)//Sorting
the digits in the ascending order using bubble sort.
                {
                                for(b=0;b<=i-a;b++)
                                {
                                                if(A[b]>A[b+1]
&& b+1<i)
                                                {
                                                                t=A[b];
                                                                A[b]=A[b+1];
                                                                A[b+1]=t;
                                                }
                                }
                }
                //printf("\nThe
possible permutations are:\n");//Permutation logic starts.
                for(j=0;j<n;j++)//Initializing
the answer array.
                {
                                B[j]=A[0];
                }
                while(move<n)//move
counts the number of times the pointer moves towards the left.
                {
                                for(j=0;j<i;j++)
                                {
                                                B[n-1]=A[j];
                                                d=0;
                                                top=-1;
                                                for(l=0;os[l]!='';l++)
                                                {
                                                                if(isalpha(os[l])!=0)
                                                                {
                                                                                top++;
                                                                                opns[top]=B[d];
                                                                                d++;
                                                                }
                                                                else
                                                                {
                                                                                value1=opns[top];
                      
                                                         //printf("\n%d",value1);
                      
                                                         top--;
                       
                                                        value2=opns[top];
                       
                                                        //printf("\n%d",value2);
                       
                                                        top--;                             
                       
                                                        if(os[l]=='+')
                      
                                                         {
                          
                                                                     if(value1==1 ||
value2==1)
                                                                                                {value=1;}
                                                                                                else
                                                                                               {value=0;}                                            
                       
                                                        }
                                                                                //else if(os[j]=='-')
                       
                                                        //{value=value1-value2;}
                       
                                                        else
if(os[l]=='.')
                       
                                                        {
                         
                                                                      if(value1==1
&& value2==1)
                                                                                                {value=1;}
                                                                                                else
                                                                                                {value=0;}
                       
                                                        }
                      
                                                         //else if(os[j]=='/')
                       
                                                        //{value=value2/value1;}
                       
                                                        //printf("\n%d",value);
                                                                                top++;
                       
                                                        opns[top]=value;
                                                                }
                                                }                              
                                               
for(d=0;d<n;d++)
                                                {
                                                                printf("%d ",B[d]);
                                                }
                                                printf("%d\n",value);
                                                num++;
                                }
                                m=n-1;
                                move=0;
                                while(m>=0
&& B[m]==A[i-1])
                                {
                                                m--;
                                                move++;
                                }
                                for(k=0;k<i;k++)
                                {
                                                if(B[m]==A[k])
                                                break;
                                }
                                B[m]=A[k+1];
                                while(m!=n-1)
                                {
                                                m++;
                                                B[m]=A[0];
                                }
                }
}

Enter the expression: (A+B).(C+D)
The truth table is
  A  B  C   D  Y
  0   0   0   0   0
  0   0   0   1   0
  0   0   1   0   0
  0   0   1   1   0
  0   1   0   0   0
  0   1   0   1   1
  0   1   1   0   1
  0   1   1   1   1
  1   0   0   0   0
  1   0   0   1   1
  1   0   1   0   1
  1   0   1   1   1
  1   1   0   0   0
  1   1   0   1   1
  1   1   1   0   1
  1   1   1   1   1

Draw the truth-table

#include<iostream>
#include<string.h>
#include<ctype.h>
char os[100];
int main()
{
                int
i=0,j=0,prcdn,prcdt,top=-1,first=0,k=0,count=0;
                char
s[100],ops[100];
                int
prcd(char a);
                void
permute(int n);
                printf("Enter
the boolean expression: ");
                scanf("%s",s);
                for(i=0;s[i]!='';i++)
                {
                                if(isalpha(s[i])!=0)
                                {
                       
                        os[j]=s[i];
                      
                          j++;                                 

















































































                                }
                                else
                               {
                                                prcdn=prcd(s[i]);
                                                if(first==0)
                       
                        {
                         
                                      top++;
                                                                ops[top]=s[i];
                                                                prcdt=prcd(ops[top]);
                                                                first=1;
                       
                        }
                      
                         else
                      
                         {
                                                                if(s[i]==')')
                                                                {
                                                                                while(ops[top]!='(')
                                                                                {
                                                                                                os[j]=ops[top];
                                                                                                top--;
                                                                                                j++;
                                                                                }
                                                                                top--;
                           
                                    }
                                                                else
if(s[i]=='(')
                                                                {
                                                                                top++;
                                                                                ops[top]=s[i];
                                                                                prcdt=prcd(ops[top]);
                                                                }
                                                                else
if(prcdn<=prcdt)
                                                                {
                                                                                while(prcdn<=prcdt
&& top>=0)
                                                                                {
                                                                                                os[j]=ops[top];
                                               
                                                j++;
                                               
                                                top--;
                                               
                                                prcdt=prcd(ops[top]);
                                                                                }
                                                                                top++;
                                                                                ops[top]=s[i];                                        



















                                                                                if(top>=0)
                                                                                {prcdt=prcd(ops[top]);}
                                                                }
                                                                else
                                                                {
                                                                                top++;
                                                                                ops[top]=s[i];
                                                                                prcdt=prcd(ops[top]);
                                                                }               



















































































































































































































































































                                                }
                                }
                }
                while(top>-1)
                {
                                os[j]=ops[top];
                                top--;
                                j++;
                }
                os[j]='';
                //printf("The postfix
equivalent is %s",os);
                for(j=0;os[j]!='';j++)
                {
                                if(isalpha(os[j])!=0)
                                {count++;}
                }                                                                                permute(count);                                                                                                                                                                                                                    
   
            scanf("%d",&i);
                return 0;
}
int prcd(char a)
{
                if(a=='(')
                {return 0;}
                else if(a=='+' || a=='-')
                {return 1;}
                else if(a=='.' || a=='/')
                {return 2;}
                else if(a=='^')
                {return 3;}
}

Friday, January 8, 2010

Power Crisis



During the power crisis in New Zealand this winter (caused by a
shortage of rain and hence low levels in the hydro dams), a
contingency scheme was developed to turn off the power to areas of the
country in a systematic, totally fair, manner. The country was divided
up into N regions (Auckland was region number 1, and Wellington number
13). A number, m, would be picked `at random', and the power would
first be turned off in region 1 (clearly the fairest starting point)
and then in every m'th region after that, wrapping around to 1 after
N, and ignoring regions already turned off. For example, if N = 17 and
m = 5, power would be turned off to the regions in the
order:1,6,11,16,5,12,2,9,17,10,4,15,14,3,8,13,7.



The problem is that it is clearly fairest to turn off Wellington last
(after all, that is where the Electricity headquarters are), so for a
given N, the `random' number m needs to be carefully chosen so that
region 13 is the last region selected.



Write a program that will read in the number of regions and then
determine the smallest number m that will ensure that Wellington
(region 13) can function while the rest of the country is blacked out.


Input and Output



Input will consist of a
series of lines, each line containing the number of regions (N) with
. The file will be terminated by a line consisting of
a single 0.



Output will consist of a series of lines, one for each line of the
input. Each line will consist of the number m according to the above
scheme.


Sample input



17
0


Sample output



7
#include<stdio.h>
#include<conio.h>
int main()
{
        int N,off[100],i,last,count=0,m,inc=0,a,num=0,ans[100];
        clrscr();
        while(scanf("%d",&N))//Taking the number of regions into N.
        {
                      if(N!=0)
                     {
                                 m=1;last=0;
                                 while(last!=13)
                                 {
                                                //Initilaizing the off[] array.
                                                for(i=0;i<N;i++)
                                                {
                                                                off[i]=0;
                                                }
                                                count=0;//Counts the number of regions in which electricity has been switched off.
                                                i=0;
                                                while(count<N)
                                                {
                                                               if(off[i]==0)
                                                               //1 denotes off and 0 denotes not off.
                                                              {off[i]=1;count++;}//Swithing off if not already off.
                                                               last=i+1;//Keeps the last region in which electricity has been switched off.
                                                               inc=0;//Counts the number of times i has been incremented by 1.
                                                               while(inc<m && count<N)
                                                               {
                                                                           if(i>=N-1)
                                                                          {
                                                                                    i=0;
                                                                                   if(off[i]!=1)                                                                            
                                                                                     {inc++;}
                                                                           }
                                                                           /*inc is incremented only while passing through a region with 0 state.*/
                                                                           if(off[i+1]==0)
                                                                           {
                                                                                       i++;
                                                                                       inc++;
                                                                           }
                                                                           else
                                                                           {
                                                                                   i++;
                                                                            }
                                                                }
                                                  }
                                                  m++;
                                    }
                                    ans[num]=m-1;//Storing the values of m in the ans[] array.
                                    num++;
                      }
                      else
                     {
                                   for(i=0;i<num;i++)
                                   {printf("%d\n",ans[i]);}
                                    scanf("%d",&m);
                                    return 0;
                  }
         }
}

17
13
0

7
1

Thursday, January 7, 2010

Factors and Factorials

All
of you are familiar with factorial of a number. The factorial of a
number N is defined as the product of all integers from 1 to N.
Mathematically it is defined as 1! = 1 and N! = N*(N-1)! But there is a
problem with this, if go for higher values of N, factorials grow very
rapidly as 6! = 720, 11! = 39916800. We can specify such large numbers
with the help of their prime factors. For example: 100 can be written
as (2 2 5 2), means there is 2 two and 2 five is present in 100.


Again
we are left with another problem. To find the prime factors of such
large numbers is very tedious task. So I want help from you people to
solve this problem. Write a code that takes an input any positive
integer N and gives output of N!’s prime factor representation.


 


Input 


Input
will consist of a series of lines, each line containing a single
integer N (2<=N<1000). The file will be terminated by a line
consisting of a single 0.


 


Output 


Output will consist of prime factor representation of N! There will be a blank line between two consecutive outputs.


 


Sample Input 


5                                                                                                                                    


7                                                                                                                                    


0


 


Sample Output 


2 3


3 1


5 1


 


2 4


3 2


5 1


7 1





















































































































































































#include<stdio.h>
int main()
{
                int
num,k=0,i=1,pri[200],j,count=0,tru=0,ans[200][3],m,n,inp,diff=0;
                //ans[][]array
stores the different prime factors and the numbers of times each occur in the
prime factorization of the factorial of the input integer.
                //clrscr();
                printf("Enter the number:
");
                scanf("%d",&inp);
                //Generating the first 100 prime
numbers and storing them in the pri[] array.
                while(count<200)
                {
                                tru=0;
                                i++;
                                for(j=2;j<=i/2;j++)
                                {
                                                if(i%j==0)
                                                {
                                                                tru=1;
                                                                break;
                                                }
                                }
                                if(tru!=1)
                                {
                                                pri[count]=i;
                                                count++;
                                }
                }
                /*printf("The prime numbers
are:\n");
                for(i=0;i<count;i++)
                {
                                printf("%d,",pri[i]);
                }*/
                n=2;
                while(n<=inp)
                {
                                 i=0;tru=0;
                                num=n;
                                 //Doing the prime factorization.
                                while(num>1)
                                {
                                                 j=0;tru=0;
                                                while(num%pri[i]==0)
                                                {
                                                                 m=0;
                                                                tru=1;
                                                                num=num/pri[i];
                                                                j++;
                                                }
                                                if(tru==1)
                                                {
                                                                if(diff==0)
                                                                 {
                            
                                                 ans[0][0]=pri[i];
                            
                                                 diff++;
                            
                                                 ans[0][1]=j;
                            
                                   }
                                                                else
                                                                {
                                                                                while(pri[i]!=ans[m][0]
&& m<diff)
                                                                                {
                                                                                              m++;
                                                                                }
                                                                                if(m==diff)
                                                                                {
                                                                                                ans[m][0]=pri[i];
                                                                                                ans[m][1]=j;
                                                                                                diff++;
                                                                                }
                                                                                else
                                                                                {
                         
                                                                      ans[m][1]=ans[m][1]+j;
                         
                                                      }
                         
                                      }
                                                }
                                                i++;
                                }
                                n++;
                }
                printf("The prime factorization
is:\n");
                //Printing the prime factorization in
the correct format.
                for(m=0;m<diff;m++)
                {
                                printf("%d
%d\n",ans[m][0],ans[m][1]);
                }
                //printf("\n");
                //getch();
                //scanf("%d",&i);
                return 0;
}



Enter the number: 5
The prime factorization is:
2 3
3 1
5 1

Enter the number: 7
The prime factorization is:
2 4
3 2
5 1
7 1

Wednesday, January 6, 2010

Train Count

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
      int days,count=0,i,j,A[100],a=0,num=0,d1h,d1m,d2h,d2m,B[100],sumh=0,summ=0;
      char str[10][10];
      printf("All day,hour and minute entries should be separated by ':' or '.'\n");
      printf("Enter the departure time of the train from the source station in hh:mm format: ");
      scanf("%s",str[0]);
      printf("Enter the departure time of the train from the destination station in hh:mm format: ");
      scanf("%s",str[1]);
      printf("Enter the journey duration from source to destination in dd:hh:mm format: ");
      scanf("%s",str[2]);
      //printf("Enter the number of days in a week the train runs from both the stations: ");
      //scanf("%d",&days);
      for(i=0;i<3;i++)
      {
            for(j=0;str[i][j]!='';j++)
            {
                  if(str[i][j]==':' || str[i][j]=='.')
                  {continue;}
                  else
                  {
                         A[a]=str[i][j]-48;
                         a++;
                         num++;
                  }
            }
      }
      i=0;j=0;
      while(i<num)
      {
             B[j]=10*A[i]+A[i+1];
             i+=2;
             j++;
      }
      B[4]=B[4]*24+B[5];
      //B[4]=2*B[4];
      B[5]=B[6];
      j--;
      d1h=B[2]-B[0];
      d1m=B[3]-B[1];
      d2h=B[0]-B[2];
      d2m=B[1]-B[3];
      //printf("\n");
      /*for(i=0;i<j;i++)
      {printf("%d ",B[i]);}*/
      //printf("\n%d %d %d %d ",d1h,d1m,d2h,d2m);
      if(d1h<0)
      {d1h=24+d1h;d1m=d1m;}
      if(d1m<0)
      {d1h=d1h-1;d1m=d1m+60;}
      if(d2h<0)
      {d2h=24+d2h;d2m=d2m;}
      if(d2m<0)
      {d2h=d2h-1;d2m=d2m+60;}
      //printf("\n%d %d %d %d ",d1h,d1m,d2h,d2m);
      while(sumh<B[4] && summ<B[5])
      {
            sumh+=d1h;summ+=d1m;
            while(summ>=60)
            {
                    summ=summ-60;
                    sumh+=1;
            }
            count++;
            if(sumh==B[4] && summ>=B[5])
            {break;}
            if(sumh>B[4])
            {break;}
            sumh+=d2h;summ+=d2m;
            while(summ>=60)
            {
                   summ=summ-60;
                   sumh+=1;
            }
            count++;
            if(sumh==B[4] && summ>=B[5])
            {break;}
            if(sumh>B[4])
            {break;}
      }
      printf("\nAssuming daily operation of the train minimum number of trains required is %d",count);
      //scanf("%d",&i);                                                                                                                   
      return 0;
}

All day hour and minute entries should be separated by ':' or '.'
Enter the departure time of the train from the source station in hh:mm format: 17:35
Enter the departure time of the train from the destination station in hh:mm format: 06:25
Enter the journey duration from source to destination in dd:hh:mm format: 01:02:15
Assuming daily operation of the train minimum number of trains required is 3

All day hour and minute entries should be separated by ':' or '.'
Enter the departure time of the train from the source station in hh:mm format: 06:25
Enter the departure time of the train from the destination station in hh:mm format: 17:35
Enter the journey duration from source to destination in dd:hh:mm format: 01:02:15
Assuming daily operation of the train minimum number of trains required is 3

Multiplication of two large numbers

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1,s2;
int i,j,k,a,c,n;
cin>>n;
while(n--)
{
cin>>s1;
cin>>s2;
int A[21000]={0},B[21000]={0};
if(s1[0]-48==0 || s2[0]-48==0)
{
cout<<"0"<<endl;
}
else
{
for(i=s1.length()-1; i>=0; i--)
{
c=0;
//zero padding.
for(k=0;k<s1.length()-1-i;k++)
{
B[k]=0;
}
for(j=s2.length()-1; j>=0; j--)
{
int a=(s1[i]-48)*(s2[j]-48)+c;
if(a>9)
{
B[k]=a%10;
c=a/10;
k++;
}
else
{
B[k]=a%10;
c=0;
k++;
}
}
if(c>0)
{
B[k]=c;
c=0;
k++;
}
for(j=0;j<k;j++)
{
int a=A[j]+B[j]+c;
if(a>9)
{
A[j]=a%10;
c=a/10;
}
else
{
A[j]=a%10;
c=0;
}
}
}
for(i=k-1;i>=0;i--)
{
cout<<A[i];
}
cout<<endl;
fflush(stdin);
}
}
return 0;
}
Enter the first number: 9999999
Enter the second number: 999
The product is:
9989999001

Monday, January 4, 2010

Minimal Swap



The basic logic is
that after swapping all the rows of the input chip-matrix are arranged in a
descending order of the ASCII values of the characters when seen from behind.



You are given an N x N matrix. The chips are represented by B and R. You can swap any two adjacent rows of the matrix. Your goal is to have all the black chips in the matrix below or on the main diagonal.


Input


The
first line of input gives the number of cases. The first line of each
test case has one integer, N. Each of the next N lines contains N
characters. Each character is either R or B.


Output 


For
each input case print the minimum number of row swaps needed to have
all the black chips in the matrix below or on the main diagonal.


 


Sample Input 


2


2


BR


BB


3


RRB


BRR


RBR


 


Sample Output 


0


2



#include<stdio.h>



#include<string.h>





int main()





{

   
char str1[10][10],dummy[10],str2[10][10];



   
int cases,i=0,n,j,m,count,l,k,ans[100];



   
scanf("%d",&cases);//Taking the number of test-cases.



   
while(i<cases)





   
{

          count=0;



          scanf("%d",&n);//Taking
the number of characters in a row.



          //Taking the chip-matrix in a 2-D
character array.



          for(j=0;j<n;j++)



          {



                                scanf("%s",str1[j]);



          }



          //Copying the input into another 2-D
character array for reversal purpose.



          for(j=0;j<n;j++)



          {strcpy(str2[j],str1[j]);}



          //Reversing the characters of each
row in the original array.



          for(m=0;m<n;m++)



          {



              k=0;



              for(l=n-1;l>=0;l--)



              {



                   str1[m][l]=str2[m][k];



                   k++;



              }



          }



          //Bubble sorting the rows in the
decreasing order.



          for(m=0;m<n;m++)



          {



              for(l=m;l<=n-m;l++)



              {



                  
if(strcmp(str1[l],str1[l+1])<0 && (l+1)<n)



                   {



                         strcpy(dummy,str1[l]);



                        
strcpy(str1[l],str1[l+1]);



                         strcpy(str1[l+1],dummy);



                         count++;



                   }



                                }



          }



          //Storing the swap-count of each case
in the ans array.



          ans[i]=count;



          i++;



   
}



   
//Printing the swap-counts.



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



   
{printf("%d\n",ans[i]);}                                                                                                                                                               



   
return 0;



}



2
2
BR
BB
3
RRB
BRR
RBR

0
2                 

Count the number of shortest path

Given a grid and two nodes (say A & B) on the grid which is h units apart horizontally and v units apart vertically, give the count of number of shortest path along the grid lines between A & B.

For example, take the case below where A & B are 1 unit apart horizontally and 1 unit apart vertically i.e. h = 1 and v = 1.
In this case, the answer is 2 as shown below:

Give your answer in terms of h and v for the generic case.



// Shortest Paths.cpp : Defines the entry point for the console application.

//

#include
#include

int h,v;
long int count=0;
int main()
{
    void path(int m,int n);
    int i,j;
    clrscr();
    printf("Enter the horizontal length: ");
    scanf("%d",&h);
    printf("Enter the vertical height: ");
    scanf("%d",&v);
    path(0,0);
    printf("The number of shortest paths is: %ld",count);
    getch();
    return 0;
}
void path(int m,int n)
{
    if(m==h || n==v)
    {
         count++;
         return;
    }
    else
    {
         path(m+1,n);
         path(m,n+1);
    }
}

Enter the horizontal length: 3
Enter the vertical height: 3
The number of shortest paths is: 20

Enter the horizontal length: 10
Enter the vertical height: 10
The number of shortest paths is: 184756

Enter the horizontal length: 2
Enter the vertical height: 1
The number of shortest paths is: 3

Enter the horizontal length: 1
Enter the vertical height: 1
The number of shortest paths is: 2

Numbers Swapping

The Problem



Given an array of numbers, your task is to swap the numbers
(except the last one) by one position backward and the last number to be put in
the first position. The constraints are that no external temporary variable can
be used.



For example if the numbers are a, b, c, d, e then the
resulting array should have a->b, b->c, c->d, d->e and e->a.



Sample input



The input consists of a line of integers separated by white
spaces. The first integer denotes the number of numbers n in the input array.
It is followed by n integers to be swapped according to the above rule.



5 12 23 34 45 56



Sample output



The output should be the line of swapped integers.



56 12 23 34 45 




// Numbers Swapping.cpp : Defines the entry point for the console application.

//


 


#include "stdafx.h"


 


 


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


{

 int A[100],n,i,j;



//A[100] is the array to store the
numbers.



//n is the number of numbers to be
swapped.



//i and j are the variables for
traversing the array.



//No other external temporary
variable is used.



while(scanf("%d",&n))



                {



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



                                {



                                                scanf("%d",&A[i]);



                                }



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



                                {



                                                for(j=i+1;j<n;j++)



                                                {



                                                                A[i]=A[i]+A[j];



                                                }



                             }



                                for(i=n-1;i>=1;i--)



                                {



                                                A[i]=A[i-1]-A[i];



                                }



                                for(i=1;i<n;i++)



                                {



                                                A[0]=A[0]-A[i];



                                }



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



                                {



                                                printf("%d
",A[i]);



                                }



                                printf("\n");



}



                return 0;



}


5 12 23 34 45 56
56 12 23 34 45

8 1 2 3 4 5 6 7 8
8 1 2 3 4 5 6 7