Wednesday, 10 April 2013

Africa 2010, Qualification Round: Store Credit(CODE JAM)

Problem

You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list (smaller number first).
Input
The first line of input gives the number of cases, N. N test cases follow. For each test case there will be:
  • One line containing the value C, the amount of credit you have at the store.
  • One line containing the value I, the number of items in the store.
  • One line containing a space separated list of I integers. Each integer P indicates the price of an item in the store.
  • Each test case will have exactly one solution.
Output
For each test case, output one line containing "Case #x: " followed by the indices of the two items whose price adds up to the store credit. The lower index should be output first.
Limits
5 ≤ C ≤ 1000
1 ≤ P ≤ 1000
Small dataset
N = 10
3 ≤ I ≤ 100
Large dataset
N = 50
3 ≤ I ≤ 2000
Sample



Input

Output
3
100
3
5 75 25
200
7
150 24 79 50 88 345 3
8
8
2 1 9 4 4 56 90 3
Case #1: 2 3
Case #2: 1 4
Case #3: 4 5


 /*http://code.google.com/codejam/contest/351101/dashboard#s=p0*/
/*download input file from above link*/
/*Output is stored in out.txt file*/
CODE:
#include<stdio.h>
int main()
{
int tc,noi[100],cri[100],a[100][100],f=0,i,j,z,l,m;
FILE *fp,*op;
op=fopen("out.txt","w");
char msg[9]="Case #";
char ch=':';
fp=fopen("Alp.in","r");
fscanf(fp,"%d",&tc);
for(i=0;i<tc;i++)
{
fscanf(fp,"%d",&cri[i]);
fscanf(fp,"%d",&noi[i]);
for(j=0;j<noi[i];j++)
fscanf(fp,"%d",&a[i][j]);

}
for(z=0;z<tc;z++)
{
    for(l =0;l<noi[z];l++)
     {
        for(m=l+1;m<noi[z];m++)
        {
            if(a[z][l]+a[z][m]==cri[z])
             {
             fprintf(op,"%s%d%c%d  %d\n",msg,z+1,ch,l+1,m+1);
               f=1;
               break;
             }
        }
         if(f)
        {
         f=0;break;
         }
     }
}
return(0);
}












Saturday, 6 April 2013

Not a Triangle

Problem Statement: 

You have N  wooden sticks, which are labeled from 1 to N. The i-th stick has a length of Li . Your friend has challenged you to a simple game: you will pick three sticks at random, and if your friend can form a triangle with them (degenerate triangles included), he wins; otherwise, you win. You are not sure if your friend is trying to trick you, so you would like to determine your chances of winning by computing the number of ways you could choose three sticks (regardless of order) such that it is impossible to form a triangle with them.

Input:

The input file consists of multiple test cases. Each test case starts with the single integer N, followed by a line with the integers L1, ..., LN. The input is terminated with N = 0, which should not be processed.

Output:

For each test case, output a single line containing the number of triples.

Example:

Input:
3
4 2 10
3
1 2 3
4
5 2 9 6
0

Output:
1
0
2

For the first test case, 4 + 2 < 10, so you will win with the one available triple. For the second case, 1 + 2 is equal to 3; since degenerate triangles are allowed, the answer is 0.


 Solution:
#include<iostream>
using namespace std;
void noways(unsigned int *a,unsigned int l);
int main()
{
unsigned int p[10][10],len[10],k=0,i;
cout<<"Enter the inputs:\n";
while(1)
{
cin>>len[k];
if(len[k]==0)
break;
for(i=0;i<len[k];i++)
cin>>p[k][i];
k++;
}
for(i=0;i<k;i++)
noways(*(p+i),len[i]);
return(0);
}


void noways(unsigned int * a,unsigned int l)
{
unsigned int i,j,k,n=0;
for(i=0;i<l;i++)
for(j=0;(j<l)&&(j!=i);j++)
for(k=0;(k<l)&&(k!=j)&&(k!=i);k++)
{
if(*(a+i) + *(a+j) < *(a+k))
n++;
if(*(a+k) + *(a+j) < *(a+i))
n++;
if(*(a+i) + *(a+k) < *(a+j))
n++;
}
cout<<"All possible ways:"<<n<<endl;
}










Wednesday, 3 April 2013

2 Player Tic Tac Toe game! (C-language)

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

char A[9]={' ' ,' ',' ',' ',' ',' ',' ',' ',' ',};
 void draw();
int main()
{
    int i,j,n,p=0;
    int status=1;
    char a[20],b[20];
    clrscr();
    printf("First player name:");
    scanf("%s",a);
    strupr(a);
    printf("Second player name:");
    scanf("%s",b);
    strupr(b);
    clrscr();
    printf("The numbers below represent the point no.\n\n");
    printf("1 | 2 | 3\n_________\n4 | 5 | 6\n_________\n7 | 8 | 9");


    for(i=0;i<9;i++)
    {
    char s;
    if(status==1)
    printf("\n\n%s\t",a);
    else
    printf("\n\n%s\t",b);
    printf("Enter the point:\n");
    scanf("%d",&n);
    n=n-1;
    if((A[n]!='X'&&A[n]!='O')&&n<9)
    {
        if(status==1)
        {
        A[n]='X';
        status=0;
        s='X';
        }
        else
        {
        A[n]='O';
        status=1;
        s='O';
        }
    }
    else
    {
        i--;
        if(n<9)
        {
        printf("\nCan't replace the previous entry\n");
        }
        else
        {
        printf("\nEnter number between 1-9\n");
        }
    }


    if(A[0]==s &&(A[4]==s &&A[8]==s))
    {
       p=1;
       break;
    }
       else if(A[2]==s &&(A[4]==s &&A[6]==s)){
       p=1;break;
       }
       else if(A[0]==s &&(A[3]==s &&A[6]==s)){
       p=1;break;

       }
       else if(A[1]==s &&(A[4]==s &&A[7]==s)){
       p=1;break;
       }
       else if(A[2]==s &&(A[5]==s &&A[8]==s)){
       p=1;break;
       }
       else if(A[0]==s &&(A[1]==s &&A[2]==s)){
       p=1;break;
       }
       else if(A[3]==s &&A[4]==s &&A[5]==s){
       p=1;break;
       }
       else if(A[6]==s &&A[7]==s &&A[8]==s){
       p=1;break;
       }
       else
       draw();
    }
    if(p==1)
    {
    if(A[n]=='x')
    printf("\n\t\tThe winner is %s \n\n",b);
    else
    printf("\n\t\tThe winner is %s \n\n",a);
    draw();
    getch();

    }
    else
    printf("\n\t\t\Draw Match\n\n");

    getch();
    return 0;
}
void draw()
{
    printf("\t\t%c | %c | %c \t\t1 | 2 | 3 \n\t\t_________\n\t\t%c | %c | %c \t\t4 | 5 | 6 \n\t\t_________\n\t\t%c | %c | %c \t\t7 | 8 | 9 \n",A[0],A[1],A[2],A[3],A[4],A[5],A[6],A[7],A[8]);
}


  

Monday, 1 April 2013

Permutation of string (without repetition)

#include<stdio.h>
#include<conio.h>
void swap (char *x,char *y);
void permit(char *a,int i,int n);
int k=0;
main()
{
char a[]="ABCDE";
clrscr();
permit(a,0,4);
printf("\n%d",k);
getch();
return(0);
}

void swap (char *x,char *y)
{
char temp;
temp=*x;
*x=*y;
*y=temp;
}

void permit (char *a,int i,int n)
{
int j;
if(i==n)          {
printf("%s\t",a);    k++;}
else
{
for(j=i;j<=n;j++)
{
swap((a+i),(a+j));
permit(a,i+1,n);
swap((a+i),(a+j));
}
}
}
                         Download C program

Saturday, 30 March 2013

De-coders: Solutions for 2nd round problem statements.

Q 1.    C program to print the pattern.

                                                                               *
                                                                             *  *
                                                                           *  *  *
                                                                         *  *  *  *
                                                                       *  *  *  *  *

#include<stdio.h>
#include<conio.h>
void main()
{
char x='*';
int i,k,l;
clrscr();
for(i=0;i<6;i++)
{
for(k=0;k<5-i;k++)
printf(" ");
for(l=0;l<i;l++)
printf(" %c",x);
printf("\n");
}
getch();
}
                                     

  Q 2.    C program to revers an integer. 

#include<stdio.h>
#include<conio.h>
void main()
{
int x,n;
clrscr();
printf("Enter the number:");
scanf("%d",&n);

while(n)
{
x=n%10;
printf("%d",x);
n=n/10;
}
getch();
}

 

  Q 3.    C program to solve two liner equations.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,l,m,n;
float x,y;
clrscr();
printf("Enter a ,b, c:");
scanf("%d%d%d",&a,&b,&c);
printf("Enter l ,m, n:");
scanf("%d%d%d",&l,&m,&n);
x=(float)(c*m-n*b)/(m*a-l*b);
y=(float)(c*l-n*a)/(b*l-m*a);
printf("x= %f\ny=%f\n",x,y);
getch();
}

  Q 4.    C program to check whether given number is some power of 2 without using any loop or % operator. 

#include<stdio.h>
#include<conio.h>
int main()
{
int a;
clrscr();
printf("Enter the number:");
scanf("%d",&a);
if(!(a&(a-1)))
printf("It is some power of 2");
else
printf("It is not some power of 2");
getch();
return(0);
}


Q 5.    C program to check whether given string is anagram of first string or not .

#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<string.h>
char S[20];
void swap(char * x,char *y);
void pert(char *a,int i,int n);
void main()
{
char a[20];
int l;
clrscr();
printf("Enter the string:");
scanf("%s",a);
printf("Enter the other string:");
scanf("%s",S);
l=strlen(a);
pert(a,0,l);
printf("NO");
getch();
}

void swap(char *x , char *y)
{
char z;
z=*x;
*x=*y;
*y=z;
}

void pert(char *a,int i,int n)
{
int j;
if(i==n)
 {
    if(strcmp(a,S)==0)
    {
    printf("YESS");
    getch();
    exit(0);
    }
 }

else
  {
    for(j=i;j<n;j++)
    {
    swap((a+i),(a+j));
    pert(a,i+1,n);
    swap((a+i),(a+j));
    }

   }
}
                 

Q 6. C program to print hello world without using semicolon.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
if(printf("Hello word"))
{}
getch();
}



Saturday, 2 March 2013

Program to convert Roman number to Decimal number

#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
char c[20];
static int p,r;
clrscr();
printf("Enter the Roman number \n");
gets(c);
for(i=0;c[i]!='\0';i++)
{
switch(c[i])
{
case 'M': sum+=1000,p=1000;break;
case 'D': sum+=500,p=500;break;
case 'C': sum+=100,p=100;break;
case 'L': sum+=50,p=50;break;
case 'X': sum+=10,p=10;break;
case 'V': sum+=5,p=5;break;
case  'I': sum+=1,p=1;break;
default:printf("Invalid entry\n");
getch();
exit(0);
}
if(r<p)
sum=sum-2*r;
r=p;
}
printf("%s is %d",c,sum);
getch();
}

OUT PUT.......?

//CODE 1
#include<iostream>
using namespace std;
int main()
{
float a=0.7;
if(a==0.7)
cout<<"C++\n";
else
cout<<"C\n";
return(0);
}


When we execute the above code we get the o/p as :"C",
But excepted o/p is :"C++" 

//CODE 2
#include<iostream>
using namespace std;
int main()
{
float a=0.7;
if(a==(float)0.7)
cout<<"C++\n";
else
cout<<"C\n";
return(0);
}



When we execute the above code we get the o/p as :"C++",

 Bcoz: Computers use a binary (0's and 1's) system to store decimal numbers. This leads to some inaccuracy, since some decimal values can't be stored exactly in binary....0.7 is stored as the value 0.69999999.Since that value isn't 0.7, the condition fails..

The float function takes care of this problem -
float(0.7)
it rounds the value 0.69999999 to 0.7. Many decimal values are stored accurately in binary, for example 0.5, but many are not.