功能:輸入一個正整數,按照從小到大的順序輸出它的所有質數的因子(如180的質數因子爲2 2 3 3 5 )
詳細描述:
函數接口說明:
public String getResult(long ulDataInput)
輸入參數:
long ulDataInput:輸入的正整數
返回值:
String
輸入描述:
輸入一個long型整數
輸出描述:
按照從小到大的順序輸出它的所有質數的因子,以空格隔開
輸入例子:
180
輸出例子:
2 2 3 3 5
#include <iostream>
using namespace std;
int main()
{
long input;
while ( cin>>input ){
while ( input != 1 ){
for ( int i=2 ; i<=input ; i++ ){
if ( input % i == 0 ){
input /= i;
cout << i << " ";
break;
}
}
}
}
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner scanner=new Scanner(System.in);
long x=scanner.nextLong();
getResult(x);
}
static void getResult(long x){//分解正數x並將質因子輸出的方法
int i=0;
int k=(int)x;
while(true){
for(i=2;i<=k;i++){
if(k%i==0){
System.out.print(i+" ");//i即爲素因子,可傳至數組另存
k=k/i;
break;
}
}
if(k==1)
break;
}
}
}