a010: 因數分解

題目:http://zerojudge.tw/ShowProblem?problemid=a010

程式碼:

import java.util.Scanner;
public class a010 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);
  while(sc.hasNext()){
   int s = sc.nextInt();
   int i = 2;
   boolean per = true;
   do{
    int c=0;
    while(s%i==0){
     c++;
     s = s/i; 
    }
    if(c>0){
     if(per){
      System.out.print(i+(c==1?"":("^"+c)));
     }else{
                        System.out.print(" * " + i + (c==1?"":("^"+c)) );
                    }
    }
    i++;
   }while(s!=1);
  }

 }

}

留言

熱門文章