质数因子

来源:互联网 发布:c语言 用法 编辑:程序博客网 时间:2024/05/30 04:29


题目描述

功能:输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )

 

 

详细描述:


函数接口说明:

    public String getResult(long ulDataInput)

输入参数:

         long ulDataInput:输入的正整数

返回值:

        String

 

 


输入描述:

输入一个long型整数



输出描述:

按照从小到大的顺序输出它的所有质数的因子,以空格隔开


输入例子:
180

输出例子:
2 2 3 3 5

#include "stdafx.h"#include<iostream>#include<vector>#include<cmath>using namespace std;int main(){unsigned long n;while (cin >> n){if (n == 1)cout << endl;else{vector<unsigned long>fac;int k = 2;while (k<=n ){while (n%k == 0){fac.push_back(k); n /= k;}k++;}for (int i = 0; i!=fac.size() ; i++)cout << fac[i] << " ";//cout << fac.back() << endl;}}return 0;}


不需要考虑是否是质数因子,因为每次得到的那个因子肯定是质数因子,其他数都可以从这些质数因子相乘得到



0 0