第一次训练赛-H

H - 分解素因子

FZU - 1075

假设x是一个正整数,它的值不超过65535(即1<x<=65535),请编写一个程序,将x分解为若干个素数的乘积。

Input

输入的第一行含一个正整数k (1<=k<=10),表示测试例的个数,后面紧接着k行,每行对应一个测试例,包含一个正整数x。

Output

每个测试例对应一行输出,输出x的素数乘积表示式,式中的素数从小到大排列,两个素数之间用“*”表示乘法。

Sample Input

2
11
9828

Sample Output

11
2*2*3*3*3*7*13

比起其它豪放又直白的题目来说,这题稍微羞涩一点,但依然是个比较简单粗暴的题。

我的方法是先迅速(用了空间换时间)求出所有可能用到的素数,然后从前往后遍历就ok了


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>

using namespace std;

bool isPrime[65535];
int prime[6550],plen=0;

int main()
{
    fill(isPrime, isPrime+65535, true);
    int i,j;
    for (i=2; i<65535; i++) {
        if (!isPrime[i])
            continue;
        prime[plen++] = i;
        for (j=i*2; j<65535; j+=i)
            isPrime[j] = false;
    }

    int t,n,now;
    scanf("%d",&t);
    while (t--) {
        scanf("%d",&n);
        now = 0;
        while (n%prime[now])
            now++;
        n /= prime[now];
        printf("%d",prime[now]);
        while (n>1) {
            while (n%prime[now])
                now++;
            while (n%prime[now]==0) {
                printf("*%d",prime[now]);
                n /= prime[now];
            }
        }
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值