质数因子
2016-03-26 15:28
302 查看
题目描述:
功能:输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )
函数接口说明: public String getResult(long ulDataInput)
输入参数: long ulDataInput:输入的正整数
返回值: String
知识点: 排序
输入: 一个long型整数
输出: 按照从小到大的顺序输出它的所有质数的因子,以空格隔开
样例输入: 180
样例输出: 2 2 3 3 5
代码:
功能:输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )
函数接口说明: public String getResult(long ulDataInput)
输入参数: long ulDataInput:输入的正整数
返回值: String
知识点: 排序
输入: 一个long型整数
输出: 按照从小到大的顺序输出它的所有质数的因子,以空格隔开
样例输入: 180
样例输出: 2 2 3 3 5
代码:
#include <iostream> #include <map> using namespace std; int isPrime(int num) { if (num <= 2) return 1; for (int i = 2; i <= sqrt(num); i++) { if (num%i == 0) return 0; } return 1; } int main() { long num,i; cin >> num; map<int, int> m; for (i = 2; i <= sqrt(num); i++) { if (isPrime(i)) m[i] = 0; } map <int, int>::iterator it, final,end; it = m.begin(); // cout << it->first << ' ' << it->second << endl; while (num>1) { while ((num % (it->first) == 0) && num > 1) { it->second++; num /= (it->first); } // cout << it->first << ' ' << it->second << endl; it++; } final = it; end = --it; for (it = m.begin(); it != final; it++) { while (it->second > 0) { cout << it->first; if (it == end&&it->second == 1) { cout << endl; system("pause"); return 0; } else cout << ' '; it->second--; } } // system("pause"); return 0; }
得分 | 运行时间 | 内存 | 复杂度 | 最大嵌套深度 |
100(100) | 16ms | 2340KB | 11 | 4 |
相关文章推荐
- 基于图的推荐算法——《推荐系统实践》
- Android中从ImageView中取出图片bitmap注意事项
- BZOJ 1806 IOI2007 Miners 矿工配餐 动态规划
- Java线程总结
- web界面设计中文字如何设计【太原UI设计师】分享
- 常见问题转载链接
- 代理模式
- 无法启动 Windows Event Log vpdn 无法启动 附带无法启动MYSQL服务”1067 进程意外终止”
- for 循环中植入两个变量
- Android studio 2.1 集成第三方短信验证类库
- podspec转podspec.json
- Scratch 介绍及入门
- 数据结构,可并堆(左偏树):COGS [APIO2012] 派遣
- byte数组和int,long之间的互相转换
- Linux上find命令详解
- DB2SQL递归写法
- PAT (Advanced Level) Practise 1104 Sum of Number Segments (20)
- 快速排序
- 搞搞hibernate.current_session_context_class
- 转 spring 常用工具类