题目:

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *...*pm^km.

输入描述:

Each input file contains one test case which gives a positive integer N in the range of long int.

输出描述:

Factor N in the format N = p1^k1 * p2^k2 *...*pm^km, where pi's are prime factors of N in increasing order, and the exponent ki is the number of pi -- hence when there is only one pi, ki is 1 and must NOT be printed out.

输入例子:

97532468

输出例子:

97532468=2^2*11*17*101*1291

思路:

本题是正整数n的质因子分解问题。n分为3种情况:

1、n=1,特殊数据,既不是质数也不能分解,直接按指定格式输出即可。

2、n是素数,不用分解,直接按指定格式输出即可。要判别n是否为质数,有多种方法,对于本题而言,最简单的方法是使用试商法。因为即使对于n=2147483647=2^31-1范围内的整数,用试商法效率也是很高的,具体参见下面给出的代码。

3、n是大于1的非质数,这正是本题要完成的工作。可以从最小的素数2开始,依次用2,3,4,5,...,sqrt(n)对n进行分解。因为当对2进行分解时,后面关于2的倍数的其他数字也就不能被n整除了,因此也就只对质数的进行计算,记得每次结束某个数字的因式分解之后,更新一下sqrt(n)。

当然,可以考虑采用筛法,事先把一定范围内的质数全部筛选出来,存入数组,然后只用这些质数去分解n,效率会相应提高很多。

(4)本题还有一点需要注意,即打印的格式。

代码:

在线测试:http://www.nowcoder.com/questionTerminal/ea8f62f661554099baed9baa471c6883?orderByHotValue=1&done=0&pos=6&onlyReference=false

AC代码:

#include<iostream>
#include<math.h> using namespace std; bool isPrime(int n){
if(n<2)
return false;
int k=int(sqrt(n+0.0));
for(int i=2;i<=k;i++){
if(n%i==0)
return false;
}
return true;
} void PrimeFactor(int n){
cout<<n<<"=";
if(n==1){
cout<<n<<endl;
return;
}
if(isPrime(n)){
cout<<n<<endl;
return;
} int k=int(sqrt(n+0.0));
int exp=0;
bool first=true; for(int i=2;i<=k;i++){
exp=0;
if(n%i==0){
while(n%i==0){
n=n/i;
++exp;
}
if(first){
if(exp>=2) cout<<i<<"^"<<exp;
else cout<<i;
first=false;
}
else{
if(exp>=2) cout<<"*"<<i<<"^"<<exp;
else cout<<"*"<<i;
}
k=int(sqrt(n+0.0));
}
} if(n>1) cout<<"*"<<n;
cout<<endl;
} int main(){
int n;
while(cin>>n){
PrimeFactor(n);
}
return 0;
}

(笔试题)质数因子Prime Factor的更多相关文章

  1. 杭电 2136 Largest prime factor(最大素数因子的位置)

    Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. 剑指Offer——美团内推+校招笔试题+知识点总结

    剑指Offer--美团内推+校招笔试题+知识点总结 前言 美团9.9内推笔试.9.11校招笔试,反正就是各种虐,笔试内容如下: 知识点:图的遍历(DFS.BFS).进程间通信.二叉查找树节点的删除及中 ...

  3. C 2010年笔试题

    1 有一个函数, 写一段程序,输入的值,输出的值. #include <stdio.h> void main() { int x,y; printf("输入x:"); ...

  4. 嵌入式Linux C笔试题积累(转)

    http://blog.csdn.net/h_armony/article/details/6764811 1.   嵌入式系统中断服务子程序(ISR) 中断是嵌入式系统中重要的组成部分,这导致了很 ...

  5. Problem 3: Largest prime factor

    The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60085 ...

  6. R语言学习——欧拉计划(3)Largest prime factor 求最大质因数

    The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60085 ...

  7. HDU 2136 素数打表+求质数因子

    Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. [暑假集训--数论]hdu2136 Largest prime factor

    Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...

  9. The largest prime factor(最大质因数)

    1. 问题: The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number ...

随机推荐

  1. openvpn部署centos7

    [root@openvpn ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 安装包 yum upgrade yum i ...

  2. 让新版Chrome支持本地跨域请求调试

    1.创建一个Chrome的启动快捷方式: 2.右键点击快捷方式属性,然后在目标路径后面,添加以下参数: --disable-web-security --user-data-dir="e:\ ...

  3. 【UOJ #112】【APIO 2015】Palembang Bridges

    http://uoj.ac/problem/112 先扣掉在同一侧的情况. 当\(k=1\)时,桥建在所有位置的中位数. 当\(k=2\)时,对于每个居民\((S_i,T_i)\),这个居民只会走离\ ...

  4. 【BZOJ1098】[POI2007]办公楼biu

    题目一开始看以为和强联通分量有关,后来发现是无向边,其实就是求原图的补图的联通块个数和大小.学习了黄学长的代码,利用链表来优化,其实就是枚举每一个人,然后把和他不相连的人都删去放进同一个联通块里,利用 ...

  5. setResult()的调用时机

    今天遇到这样一个问题,我在Activity-A中用startActivityForResult()方法启动了Activity-B,并且在B中通过setResult()方法给A返回值,由于某些原因不能在 ...

  6. Navicat连接Docker中的mysql报错:client does not support authentication

    1.进入mysql容器中 docker exec -it mysqltest(mysql容器名称) bash 2.进入mysql数据库 mysql -uroot -p 3.输入mysql密码 4.远程 ...

  7. LCA POJ 1330 Nearest Common Ancestors

    POJ 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24209 ...

  8. web前端 -- onkeydown、onkeypress、onkeyup、onblur、onchange、oninput、onpropertychange的区别

    FROM:http://www.cnblogs.com/svage/archive/2011/11/15/2249954.html onkeydown:按下任何键(字母.数字.系统.tab等)都能触发 ...

  9. Scala 元组

    与列表一样,元组也是不可变的,但与列表不同的是元组可以包含不同类型的元素. 元组的值是通过将单个的值包含在圆括号中构成的.例如: val t = (1, 3.14, "Fred") ...

  10. SilverLight学习笔记--使用WebClient实现通讯(一)(上传和下载字符串数据)

    一.什么是WebClient类   1.基本知识    WebClient类是Mircsoft在.NET框架下提供的向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法.通过这个类 ...