i guess Sunny has already mentioned (concatenating the two numbers and
comparing) this technique before, i liked and tried coding it ... it works
perfectly without comparing the second digit incase the first is same. the
algorithm can run in O(nlogn) taking the best sorting technique , though i
have used the O(n^2)one.
i tried for 5 numbers, one can do it for N numbers too.
@maksym could you explain your logic
int length(int );
int power(int );
int com(int, int );
void swap(int *, int *);
main()
{
int a[5],l, j, temp;
int i ;
for(i = 0 ;i<5;i++)
{
printf(" \n\tEnter the %d number ", i+1);
scanf("%d",&a[i]);
l =length(a[i]);
}
for (i = 0; i<5; i++)
for (j = 0 ; j<5;j++)
{
temp = com(a[i], a[j]);
if (temp != a[i])
swap(a+i, a+j);
}
for (i = 0 ;i<5;i++)
printf("\n\t%d",a[i]);
printf("\n\t\t The largest possible number is\n\t ");
for (i = 4 ;i>=0;i--)
printf("%d",a[i]);
getch();
}
void swap(int *n1, int *n2)
{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
int length(int a)
{
int len=0,i = 10 ;
while (a!=0)
{
a = a/10;
len++;
}
return len;
}
int com(int a1, int a2)
{
int l1, l2;
int c,d;
l1 = length(a1);
l2 = length(a2);
c= a1*power(l2) +a2;
d = a2*power(l1)+a1;
if (c>d)
return a1;
else
return a2;
}
int power(int n)
{
int i=0, a = 1;
for (i = 0;i<n;i++)
a *=10;
return a;
}
enjoy :)
On Mon, May 30, 2011 at 1:36 PM, Sudhir mishra <[email protected]>wrote:
> give some explanation
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.