HDU 1164 Eddy's research I(素数)
来源:互联网 发布:绿色破解版软件 编辑:程序博客网 时间:2024/06/09 16:44
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1164
题 意:将n分解为素因子的形势。
思 路:
一:将 素数打表再暴力求解。
代码如下:
#include <iostream>using namespace std;#include <string.h>#include <stdio.h>#include <climits>#include <algorithm>#define maxn 65535typedef __int64 LL;int a[maxn];int prime[maxn];int main(){ memset(a,1,sizeof(a)); for( int i = 2; i <= maxn; i ++ ) { if( a[i] ) for( int j = i+i; j <= maxn; j += i ) a[j]=0; } int j = 0; for( int i = 2; i < maxn; i ++ ) { if( a[i] ) prime[j] = i,j++; } int n; while( scanf ( "%d", &n ) != EOF ) { int f = 1; for( int i = 0; i < j; i ++ ) { if( n == 1 ) break; while( n%prime[i] == 0 ) { if( f ) { printf("%d",prime[i]); f=0; } else printf("*%d",prime[i]); n /= prime[i]; } } printf("\n"); } return 0;}二: 打表后用DFS遍历求解。
代码如下:
#include <iostream>using namespace std;#include <string.h>#include <stdio.h>#include <climits>#include <algorithm>#define maxn 65535typedef __int64 LL;int a[maxn], n, k;int prime[maxn];void DFS( int x ){ if( x == 0 ) return ; for( int i = 0; i < k; i ++ ) { if( x%prime[i] == 0 ) { printf( x==n?"%d":"*%d",prime[i] ); DFS( x/prime[i] ); break; } }}int main(){ memset(a,1,sizeof(a)); for( int i = 2; i <= maxn/2; i ++ ) { if( a[i] ) for( int j = i+i; j <= maxn; j += i ) a[j]=0; } k = 0; for( int i = 2; i < maxn; i ++ ) { if( a[i] ) prime[k++] = i; } while( scanf ( "%d", &n ) != EOF ) { DFS( n ); printf("\n"); } return 0;}
0 0
- HDU 1164 Eddy's research I(素数)
- HDU 1164 Eddy's research I(素数连乘)
- HDU-Eddy's research I-拆分素数
- HDU 1164 Eddy's research I【素数筛选法】
- HDU 1164 Eddy's research I 素数因子
- HDU 1164 Eddy's research I(埃拉托斯尼斯筛法求素数)
- hdu 1164 Eddy's research I 打印素数表
- HDU 1164 Eddy's research I(将数分解成素数之积)
- HDU 1164 Eddy's research I
- HDU 1164 Eddy's research I
- hdu 1164 Eddy's research I
- HDU 1164 Eddy's research I
- hdu 1164 Eddy's research I
- Hdu 1164-Eddy's research I
- hdu(1164)Eddy's research I
- HDU 1164 Eddy's research I
- hdu 1164 Eddy's research I
- hdu 1164 Eddy's research I
- C++手稿:函数与参数
- 关于visual c++6.0【未完待续】
- Android错误之--activity_main cannot be resolved or is not a field
- leetcode 139: Word Break
- 数据结构的各种排序算法稳定性比较
- HDU 1164 Eddy's research I(素数)
- zookeeper+kafka配置
- Android SERVICE后台服务进程的守护
- CSS规则的优先级匹配
- new到构造方法
- DH密钥交换算法
- JavaScript indexOf() 方法
- Java内部类大解析
- 暑假-动态规划 III-D - FatMouse and Cheese