#9774: How can I improve this source code? A010


wa007123456 (SDK)


 Sorry that I can't type Chinese now

 I have learned Java for 3 weeks already

 So that means I am a novice

  But As title says!  

 The answer is correct

  but it takes too much time to solve it

 <<< Exercise A010>>>

 Here is my way to solve it

   1.Create the prime number list ( number[] )

    2. if the prime number can completely divide the inputing number

       (upperp[]) which is related to it increase 1 point.

   3.Combining the number to a string

   4.  Show the answer

 

   I think that the array is too large ,so it takes so much  time to finish its job.

   and even some elements in the array(number[]) are zero

   but I have no idea how to improve it....(It always shows TLE)

   if someone could modify my code to a better pattern

   I will appreciate your kindness.

   plz help me :(

---------------------Source code-------------------

import java.util.Scanner;

class MyJava{
   
    public static void main(String[] args){
       
        Scanner StdIn =new Scanner(System.in);
     
      
         int total=1000000;
        int count=0;
       
       
        int[] number=new int[total];
       int[] upper=new int[total];
        int num=0;
      
        String tmp_msg="";
         String msg="";
         
       int j;
        for(int i=0;i<total;i++){
            for(j=2;j<i;j++){
                if(i%j==0){
                   
                    break;
                }
            }
            if(i==j){
                number[i]=j;
               
            }
        }
        
        while(StdIn.hasNext()){
              num=StdIn.nextInt();
           
              System.out.println("test");
                for(int i=0;i<total;i++){
                   
                    if(number[i]==0)
                        continue;
                   
                    while(num!=0&&(num%number[i])==0){
                        upper[i]++;
                        num/=number[i];
                    }
                   
                }
               
           
           
           
            count=0;
            while(count<total){
               
               
           
                if(upper[count]!=0&&number[count]!=0){
                    tmp_msg+=(number[count]);
                    if(upper[count]!=1)
                        tmp_msg+="^"+upper[count];
                   
                    tmp_msg+=" * ";
                }
               
                count++;
               
             
            }
           
            msg=tmp_msg.substring(0,tmp_msg.length()-3);
            tmp_msg="";
            for(int i=0;i<upper.length;i++){
                upper[i]=0;
            }
            System.out.println(msg);
           
        }
               
           
        }
    }



 

#9787: Re:How can I improve this source code? A010


jeffandhappy (苦力怕)


 Sorry that I can't type Chinese now

 I have learned Java for 3 weeks already

 So that means I am a novice

  But As title says!  

 The answer is correct

  but it takes too much time to solve it

 <<< Exercise A010>>>

 Here is my way to solve it

   1.Create the prime number list ( number[] )

    2. if the prime number can completely divide the inputing number

       (upperp[]) which is related to it increase 1 point.

   3.Combining the number to a string

   4.  Show the answer

 

   I think that the array is too large ,so it takes so much  time to finish its job.

   and even some elements in the array(number[]) are zero

   but I have no idea how to improve it....(It always shows TLE)

   if someone could modify my code to a better pattern

   I will appreciate your kindness.

   plz help me :(

---------------------Source code-------------------

import java.util.Scanner;

class MyJava{
   
    public static void main(String[] args){
       
        Scanner StdIn =new Scanner(System.in);
     
      
         int total=1000000;
        int count=0;
       
       
        int[] number=new int[total];
       int[] upper=new int[total];
        int num=0;
      
        String tmp_msg="";
         String msg="";
         
       int j;
        for(int i=0;i            for(j=2;j                if(i%j==0){
                   
                    break;
                }
            }
            if(i==j){
                number[i]=j;
               
            }
        }
        
        while(StdIn.hasNext()){
              num=StdIn.nextInt();
           
              System.out.println("test");
                for(int i=0;i                   
                    if(number[i]==0)
                        continue;
                   
                    while(num!=0&&(num%number[i])==0){
                        upper[i]++;
                        num/=number[i];
                    }
                   
                }
               
           
           
           
            count=0;
            while(count               
               
           
                if(upper[count]!=0&&number[count]!=0){
                    tmp_msg+=(number[count]);
                    if(upper[count]!=1)
                        tmp_msg+="^"+upper[count];
                   
                    tmp_msg+=" * ";
                }
               
                count++;
               
             
            }
           
            msg=tmp_msg.substring(0,tmp_msg.length()-3);
            tmp_msg="";
            for(int i=0;i                upper[i]=0;
            }
            System.out.println(msg);
           
        }
               
           
        }
    }