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