題目連結:a010: 因數分解
JAVA 程式碼
//作者:骷骷 //網誌:http://bekachu.pixnet.net/blog import java.util.Scanner; public class JAVA { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s; while(sc.hasNextInt()){ s=sc.nextInt(); int i=2; boolean first=true; do{ int c=0; while(s%i==0){ c++; s/=i; } if(c>0){ if(first){ System.out.print( i + (c==1?"":("^"+c)) ); first=false; }else{ System.out.print( " * " + i + (c==1?"":("^"+c)) ); } } i++; }while(s!=1); System.out.println(); } } } /*測試資料: 20 2^2 * 5 17 17 999997 757 * 1321 */
==========
其實這題骷骷本來沒有要記錄的,
因為還蠻簡單的,
只是如果題目的輸出和輸入相反時,
原本的程式碼就可以派上用場,
骷骷怕到時轉不過來,
最後還是決定記錄下來,
可以提供一個思考的方向。
兒童節+清明節連假,
骷骷這禮拜可以多多記錄囉!
文章標籤
全站熱搜