CDOJ--1237

原体连接:http://acm.uestc.edu.cn/problem.php?pid=1237

分析:质因子单增;在寻找下一个质因子时,从前一个开始。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 int isprime(int s,int n)
 7 {
 8     int i;
 9     for(i=s;i<=sqrt(n);i=i+2)
10     {
11         if(n%i==0)return i;
12     }
13     return 1;
14 }
15 int main()
16 {
17     int t,n,m,ans[1000];
18     scanf("%d",&t);
19     while(t--)
20     {
21         int cnt=0,p;
22         memset(ans,0,sizeof(ans));
23         scanf("%d",&n);
24         m=n;
25         while(n%2==0)
26         {
27             ans[cnt++]=2;
28             n/=2;
29         }
30         if(n>2)
31         for(int i=3;;)
32         {
33             if(n==1)break;
34             if(n%i==0)
35             {
36                 ans[cnt++]=i;
37                 n/=i;
38             }
39             else
40             {
41                 p=isprime(i,n);
42                 if(p==1)
43                 {
44                     ans[cnt++]=n;
45                     break;
46                 }
47                else i=p;
48             }
49         }
50         printf("%d=%d",m,ans[0]);
51         for(int i=1;i<=cnt-1;i++)
52         printf("*%d",ans[i]);
53         printf("\n");
54     }
55     return 0;
56 }

 

转载于:https://www.cnblogs.com/i-love-acm/p/3150029.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值