求输入数字的所有质因数,并将所有质因数进行排序,并以质因数+空格的形式输出
#include <iostream> #include <string> #include <math.h> using namespace std; static int cou; int result[200] = {0}; void Rank(int add[],int n) //将求得的质因数进行冒泡排序,从小到大 { int i,j,num; for(i=0;i<n-1;i++){ for(j=0;j<=n-i-2;j++){ if(add[j]>add[j+1]) { num = add[j]; add[j] = add[j+1]; add[j+1] = num; } } } } void getResult(long num) //求质因数算法 { cou = 0; for(int i = 2;i <= num;i++) { if(num%i == 0){ result[cou++] = i; num = num/i; i = 1; } } Rank(result,cou); } int main() { long int num; scanf("%d",&num); getResult(num); for(int i = 0;i < cou;i++) { printf("%d ",result[i]); } printf(" "); return 0; }