Monday, February 21, 2011

SOLUTION OF QUESTION 1

import java.io.*;

class numcombi

{

String str="";

int a=0;

int b=0;

public void combi() throws IOException

{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a number : ");

int num = Integer.parseInt(br.readLine());

System.out.println("Combinations :");

for(int i=1;i<=num/2;i++)

{

str="";

extract(num,i);

}

}

public void extract(int n, int m)

{

a=m;

str+=" "+a;

b=n-a;

System.out.println(str+" "+b);

if(b<=m)

{

return;

}

extract(b,m);

}

public static void main(String arg[]) throws IOException

{

numcombi obj= new numcombi();

obj.combi();

}

}

14 comments:

Anonymous said...

sir......the output that i an getting for this solution is not the answer which is given in the sample output.The order is different.

the sample output is suppose to be:


for Input N=5
Output : Given number 5
Combinations :
1 1 1 1 1
1 1 1 2
1 2 2
1 1 3
1 4
2 3

but the output that i an getting is:


Enter a number : 5
Combinations :
1 4
1 1 3
1 1 1 2
1 1 1 1 1
2 3
2 2 1

Anonymous said...

wat kind of doubt is it??? dude dont worry the output u gt is correct... the order does nt matter dont b stupid!

Unknown said...

output is same
only the order is different.
the recursive function is printing the output in this form

Anonymous said...

bt ma frnd...the ordr mattrs....
u shld prnt xactly wats gvn in the sample o/p

Anonymous said...

yes the order matters in the print out dat u take the sample out put is very inpt.......the examiner checks 4 the sample out put first.....and dat shud b in da correct ordr.....

Anonymous said...

So wait. Is there a corrected program for this?

Anonymous said...

yup...........my frnd said dat she'll send it to me but den she didnt send it till nw...........does ny 1 kno da soln 4 these.......


The Hamming distance between two bit patterns of same length is the number of bit positions at which they (bits) differ. For example, the Hamming distance between 11101010 and 00011001 is 6, since they differ at 1st , 2nd, 3rd , 4th, 7th, and 8th positions.
Write a program, which inputs 2 non-negative integers, determines their binary representation and outputs the Hamming distance between their binary representations.



Given a time in numbers we can convert it into words. For example
5 : 00 Five o'clock
5 : 10 Ten minutes past Five
5 : 15 Quarter past Five
5 : 30 Half past Five
5 : 40 Twenty minutes to Six
5 : 45 Quarter to Six
5 : 47 Thirteen minutes to Six
Write a program which first inputs two integers, the first between 1 to 12 (both inclusive) and second between 0 to 59 (both inclusive) and prints out the time they represent, in words. Your program should follow the format of the examples above.

SAMPLE DATA :
Input Time : 3,0
Output 3 : 00 Three o'clock
Input Time : 7,29
Output 7 : 29 Twenty nine minutes past Seven
Input Time : 6,34
Output 6 : 34 Twenty six minutes to Seven
Input Time : 12,1
Output 12 : 01 One minute past Twelve
Input Time : 12,45
Output 12 : 45 Quarter to one
Input Time : 10,59
Output 10 : 59 One minutes to Eleven
Input Time : 14,60
Output Incorrect input

Anonymous said...

Yeah the hamming one is pretty easy. Try it yourself? Just take some time and you'll get the solution. And when your friend sends it, do you mind posting it here?

Anonymous said...

thnx........i am waitin 4 her 2 send it.....but den its too late nw.......may b som thing wnt wrong wid her net.......i dnt think she'll send it nw........:(....

Anonymous said...

Oh great. :| Thanks for the help anyway.
The hamming program - just try digit extraction and comparing like that. Just use a counter to keep adding the number of unequal terms. That's about it, right?
Or you can turn it into a string, and then compare character-wise.

Jojo said...

import java.io.*;
public class Time
{
int hour,mint;
public Time()
{
hour=0;
mint=0;
}
public Time(int h,int m)
{
hour=h;
mint=m;
}
public void accept()throws IOException
{
System.out.println("Input Time :");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
hour=Integer.parseInt(br.readLine());
System.out.print(". ");
mint=Integer.parseInt(br.readLine());
}
public void cal()throws IOException
{
String ho[]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve"};
String mn[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve",
"Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen",
"Nineteen","Twenty","Twentyone","Twentytwo","Twentythree","Twentyfour",
"Twentyfive","Twentysix","Twentyseven","Twentyeight","Twentynine","Thirty"};
if(hour <0 || hour > 24)
{
System.out.println(" Incorrect Output");
return;
}
else if(mint<0||mint>60)
{
System.out.println(" Incorrect Output");
return;
}
else if(hour>12&&hour<24)
{
hour=hour-12;
}
switch(mint)
{
case 0:
System.out.println("Input :"+hour+"."+mint);
System.out.println("Output :"+ho[hour-1]+" O'clock");
break;
case 15:
System.out.println("Input :"+hour+"."+mint);
System.out.println("Output :"+" quater past "+ho[hour-1]);
break;
case 30:

System.out.println("Input :"+hour+"."+mint);
System.out.println("Output :"+" half past "+ho[hour-1]);
break;
case 45:

System.out.println("Input :"+hour+"."+mint);
System.out.println("Output :"+" quater to "+ho[hour]);
break;
default:
{
if(mint<30)
{
System.out.println("Input :"+hour+"."+mint);
System.out.println("Output :"+mn[mint]+" past "+ho[hour-1]);
}
else
{
System.out.println("Input :"+hour+"."+mint);
System.out.println("Output :"+mn[60-mint]+" to "+ho[hour]);
}
}
}
}
public void main()throws IOException
{
Time obj=new Time();
obj.accept();
obj.cal();
}
}

Jojo said...

import java.io.*;
class HD
{
int x,y,hd=0;
int numdig=0;
public void input()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the 1st no: ");
x=Integer.parseInt(br.readLine());
System.out.println("Enter the 2nd no: ");
y=Integer.parseInt(br.readLine());
}
public void hamming()
{
int binx=bin(x);
int X[]=new int[numdig];
int m=binx;
for(int i=0;i0)
{
r=r*10+(m%2);
m=m/2;
numdig++;
}
return r;
}

public void main()throws IOException
{
HD ob=new HD();
ob.input();
ob.hamming();

}
}
There is better way than this.
This prg is not fully correct but Time prg is correct

Anonymous said...

Does anyone know what exactly is coming?And the order is important for this one,right?.Can someone post it here?

Anonymous said...

hw was da xam??????...........me vry happy wid wat evr came..:):):):).........
@jojo.....thnx 4 ur efforts in puttin the prog up................