当前位置:网站首页>HDU Largest prime factor(埃拉托色尼筛选法求素数模板法改动)
HDU Largest prime factor(埃拉托色尼筛选法求素数模板法改动)
2022-08-03 14:34:00 【51CTO】
题目地址: 点击打开链接
题意:给你一个数,求它这个数的最大素因子在素数表的第几位
思路:刚开始思路有一点错误,看错误代码
错误代码:
#include <iostream>0
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
typedef long long ll;
using namespace std;
const int maxn = 1e6 + 10;
int isprime[ maxn];
void init()
{
int i, j;
int nprime = 0;
memset( isprime, - 1, sizeof( isprime));
isprime[ 1] = 0;
for( i = 2; i < maxn; i ++) //刚开始程序老是崩溃,后来开小就不会崩溃,但实际上我觉得应该开到maxn,AC的程序也开到了maxn
{
if( isprime[ i] == - 1)
{
nprime ++;
isprime[ i] = nprime;
for( j = i * i; j < maxn; j += i) //这里和打素数表有一点不同不是从i*i开始的,要是这样写程序会奔溃,
{
isprime[ j] = nprime;
}
}
}
}
int main()
{
int n;
init();
while( scanf( "%d", & n) != EOF)
{
printf( "%d\n", isprime[ n]);
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
AC代码1:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
typedef long long ll;
using namespace std;
const int maxn = 1e6 + 10;
int isprime[ maxn];
void init()
{
int i, j;
int nprime = 0;
memset( isprime, - 1, sizeof( isprime));
isprime[ 1] = 0;
for( i = 2; i < maxn; i ++)
{
if( isprime[ i] == - 1)
{
nprime ++;
for( j = i; j < maxn; j += i)
{
isprime[ j] = nprime;
}
}
}
}
int main()
{
int n;
init();
while( scanf( "%d", & n) != EOF)
{
printf( "%d\n", isprime[ n]);
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
AC代码2:
#include<stdio.h>
int a[ 1000000] ={ 0};
int main()
{
int i, j, l = 0, n;
a[ 1] = 0;
for( i = 2; i < 1000000; i ++)
{
if( ! a[ i])
{
l ++;
for( j = i; j < 1000000; j += i)
{
a[ j] = l;
}
}
}
while( scanf( "%d", & n) != EOF)
{
printf( "%d\n", a[ n]);
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
程序更简洁
边栏推荐
猜你喜欢
随机推荐
【实战】Next.js + 云函数开发一个面试刷题网站
varchar2 and varchar2(char)_datetime data types
币圈提款机:Solana钱包出现未知安全漏洞 大量用户数字资产被盗
Role usage in Ansible
PAT乙级-B1013 数素数(20)
致一位湖南女孩
Ansible中的角色使用
Huffman树
The difference between servlet and jsp _ the difference between servlet and class
阿里大牛最新总结分享的高并发编程核心笔记(终极版),高并发系统架构场景一应俱全
数字孪生的4个最佳实践
爱可可AI前沿推介(8.3)
PAT乙级-B1008 数组元素循环右移问题(20)
20220801使用安信可的ESP-01S模块实现WIFI的UART传输功能
【R语言科研绘图】--- 柱状图
UE4 解决C盘缓存问题
项目管理:PMP和IPMP哪个更值得考?两个证书的区别在于哪里?
用1000行代码统计西安新房价格后,我有一个惊人的发现……
PAT乙级-B1014 福尔摩斯的约会(20)
选择合适的 DevOps 工具,从理解 DevOps 开始