华为机试 质数因子

题目描述

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

最后一个数后面也要有空格
 

详细描述:


函数接口说明:

public String getResult(long ulDataInput)

输入参数:

long ulDataInput:输入的正整数

返回值:

String

输入描述:

输入一个long型整数

扫描二维码关注公众号,回复: 101455 查看本文章

输出描述:

按照从小到大的顺序输出它的所有质数的因子,以空格隔开。最后一个数后面也要有空格。

示例1

输入

180

输出

2 2 3 3 5

最后一个也是空格!
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef  long long ll;
int a[10010];
int main()
{

        ll n;
        scanf("%lld",&n);
        int i=2;
        int k=0;
        while(i*i<=n)
        {
            if(n%i==0)
            {
                n/=i;
                a[k++]=i;
                i=1;
            }
            i++;
        }
        a[k++]=n;
      //  string s;
        for(int i=0;i<k;i++)
        {
            printf("%d%c",a[i],' ');
        }
}

  

猜你喜欢

转载自www.cnblogs.com/masterchd/p/8982170.html