题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1164
思路:每次除小于它的数就行,不用先筛一遍素数,刚开始筛素数的时候下标超int了,头一次遇到,因为任何一个数都能分解成素数之积,举个例子啊,如果能被4整除
AC代码1:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
const int maxn = 65540;
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
bool isprime[maxn+10];
int a[maxn+10];
int k;
void doprime()
{
k = 0;
memset(isprime,true,sizeof(isprime));
for(long long i=2; i<=maxn; i++)
{
if(isprime[i])
{
a[++k] = i;
for(long long j=i*i; j<=maxn; j+=i)//要用long long不然乘起来会超Int直接就爆了
{
isprime[j] = false;
}
}
}
}
int main()
{
int n;
doprime();
while(scanf("%d",&n) != EOF)
{
bool flag = true;
for(int i=1; i<=k; i++)
{
if(n == 1)
break;
while(n % a[i] == 0)
{
if(flag)
{
printf("%d",a[i]);
flag = false;
}
else
{
printf("*%d",a[i]);
}
n /= a[i];
}
}
printf("\n");
}
return 0;
}
AC代码2:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
const int maxn = 65540;
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
bool flag = true;
for(int i=2; i<=n; i++)
{
if(n == 1)
break;
while(n % i == 0)
{
if(flag)
{
printf("%d",i);
flag = false;
}
else
{
printf("*%d",i);
}
n /= i;
}
}
printf("\n");
}
return 0;
}