well only a few things are wrong with the code but you do have some
necessary code commented out too....



On Oct 3, 10:08 pm, mandy <[email protected]> wrote:
> here is my program and i want to return in sorting order.
> input=42315
> output=12345
> and here is my program.......
>
> public class Lottery
> {
>     static String str="42135";
>     int ar[]=new int[20];
>     int i=0,j=0,k=0;
>     int temp=0;
>     public int[] get(int[] p)
>     {

>         System.out.print(p[i]);              You should probably comment this 
> out when you are not debugging.... As Mr. Amahdy said this would generate 
> confusing output...

>         i++;
>         if(i==str.length())
The thing is you are trying to call this function again and again when
the method you have will work on the complete string at a single
go...So you should probably comment out these two lines too...

>             for(j=0;j<str.length()-1;j++)
>         {
>             for(i=j+1;i<str.length();i++)

>                 if(p[j]<p[i])                  If you compare this way the 
> corresponding output would be in a descending order as he said.... You can 
> just reverse the sign here

>                 {
>                     temp=p[j];
>                     p[j]=p[i];
>                     p[i]=temp;
>
>                 }
>
>         }

>         //if(j==str.length())

>           return p;
>       }

>     public static void main(String arg[])
>     {
>         Lottery ob=new Lottery();
>
>         int l=0;
>         int a[]=new int[5];

>         int j[]=new int[5];             I don't think you require a new 
> variable to store the result from the get function as the array inside is 
> already changed....

>         try
>         {
>         for(int i=0;i<a.length;i++)

>         {
>             a[i]=str.charAt(i)-'0';
Well if you just use this method for the complete string rather than
one at a time you would get an integer array which can then be
directly passed to the get function. Hence i think you can comment out
the starting braces....

>
>         //for(l=0;l<a.length;l++)

>         j=ob.get(a);                   This line will do the job just fine 
> even if it written outside the for loop

>          System.out.print(j[l]);         You can then simply write this line 
> in a for loop similar to one commented out earlier  //for(l=0;l<a.length;l++)

>               l++;              This would be a silly error since if you add 
> this line one element will be skipped each time in the loop

>         }
>         }
>         catch(Exception e)
>     {
>         System.out.println("got excep:"+e);
>     }
> }
> }



So I think the complete code should look like this


public class Lottery
{
    static String str="42135";
    int ar[]=new int[20];
    int i=0,j=0,k=0;
    int temp=0;
    public int[] get(int[] p)
    {
        for(j=0;j<str.length()-1;j++)
        {
             for(i=j+1;i<str.length();i++)
                 if(p[j]>p[i])
                 {
                      temp=p[j];
                      p[j]=p[i];
                      p[i]=temp;
                 }
        }
        return p;
    }

    public static void main(String arg[])
    {
        Lottery ob=new Lottery();

        int l=0;
        int a[]=new int[5];
        int j[]=new int[5];
        try
        {
            for(int i=0;i<a.length;i++)
                a[i]=str.charAt(i)-'0';
            j=ob.get(a);
            for(l=0;l<a.length;l++) {
                System.out.print(a[l]);
            }
        }
        catch(Exception e)
        {
            System.out.println("got excep:"+e);
        }
    }
}

Thanks and Regards,
Vikram

-- 
You received this message because you are subscribed to the Google Groups 
"google-codejam" 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/google-code?hl=en.

Reply via email to