HDU 1164 Eddy's research I(素因子拆分)
2016-05-19 15:48
411 查看
题目地址: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;
}
思路:每次除小于它的数就行,不用先筛一遍素数,刚开始筛素数的时候下标超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;
}
相关文章推荐
- Extjs4文件上传
- okHttp 使用HTTPS证书
- 关于禁止ViewPager预加载问题,完美解决!
- C++ limits头文件
- 【转载】SQLServer日期格式化
- CoffeeScript 更优美的Javascript
- Digit Recognition via CNN
- 创建一个对象,字母存储在一个数组中,并且用一个方法可以将字母连在一起,显示成一个单词
- HttpClient发送文件与报文
- 二叉树、平衡二叉树、完全二叉树、满二叉树 .
- nginx+tomcat+memcached
- SQL 双亲节点查找所有子节点的实现方法
- spec06 sucks
- Unity中的C#内存管理(三)
- 表单序列化
- 安装SQL Server 2008时提示重启计算机失败!
- 2016-5-19
- HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求。
- 页面轮换,ViewFlipper 和 ViewPager 的区别
- .tar.gz的解压