Ural 1014-The Product of Digits
2016-04-26 00:00
363 查看
摘要: 对Ural 1014-The Product of Digits的C++实现。
【问题描述】
Your task is to find the minimal positive integer number Q so that the product of digits of Q is exactly equal to N.
【解题思路】
分解N的因子的度量标准:尽量分解出大因子。
注意:两个特例,N=0时Q=0,N=1时Q=1。
否则采用贪心策略,按照从9到2的顺序分解N的因子:先试将N分解出尽量多的因子9,再试分解出尽量多的因子8···。若最终分解后的结果不为1,则无解;否则因子从小到大组成最小的正整数Q。
【具体实现】
【额外补充】
clear()并不真正释放内存(这是为优化效率所做的事),clear实际所做的是为vector中所保存的所有对象调用析构函数(如果有的话), 然后初始化size这些东西,让觉得把所有的对象清除了。
【问题描述】
Your task is to find the minimal positive integer number Q so that the product of digits of Q is exactly equal to N.
Input
The input contains the single integer number N (0 ≤ N ≤ 109).Output
Your program should print to the output the only number Q. If such a number does not exist print −1.Sample Input
10
Sample Output
25
【解题思路】
分解N的因子的度量标准:尽量分解出大因子。
注意:两个特例,N=0时Q=0,N=1时Q=1。
否则采用贪心策略,按照从9到2的顺序分解N的因子:先试将N分解出尽量多的因子9,再试分解出尽量多的因子8···。若最终分解后的结果不为1,则无解;否则因子从小到大组成最小的正整数Q。
【具体实现】
#include <iostream> #include <vector> #include <algorithm> /*输入输的上限*/ #define maxNum 1000000000 using namespace std; /*标志是否已经分解完成*/ int sign; /*保存分解后的因数*/ vector<int> RES; /*分解输入的数*/ void Fun(int num) { if (sign == 1) return; /*如果传入的数只有个位数,则返回。递归的终点*/ if (num < 10) { RES.push_back(num); sign = 1; return; } /*按从9到2的顺序分解nu 3ff0 m的因子*/ int i; for (i = 9; i >= 2; i--) { if (!(num % i)) { RES.push_back(i); Fun(num / i); if (sign == 1) return ; } } } int main() { int num; while (cin >> num && 0 <= num && num <= maxNum) { /*特殊值*/ if (!num) cout << "10" << endl; else { sign = 0; /*初始化容器RES,清零*/ RES.clear(); /*分解输入的数*/ Fun(num); /*按照从小到大的顺序排序,则可保证输出的数最小*/ sort(RES.begin(), RES.end()); if (sign == 1) { for (int i = 0; i < RES.size(); i++) cout << RES[i]; } /*如果不存在*/ else cout << "-1"; cout << endl; } } return 0; }
【额外补充】
clear()并不真正释放内存(这是为优化效率所做的事),clear实际所做的是为vector中所保存的所有对象调用析构函数(如果有的话), 然后初始化size这些东西,让觉得把所有的对象清除了。
相关文章推荐
- Ural 1025-Democrary in Danger
- UVa 591-Box of Bricks
- 移动端开发问题集锦
- dml--数据操控语句
- javascript锚点
- 利用sqoop1.6对mysql和hive进行同步的问题
- 深入理解Java的接口和抽象类
- HTTP 状态代码及其定义。
- solr5.5.0的安装配置①☞solr简单安装(linux)
- solr5.5.0的安装配置②☞solr创建core(linux)
- solr5.5.0安装配置③☞mysql数据库导入solr的core(linux)
- jQuery html5Validate基于HTML5表单验证插件
- rspamd 使用redis 作为bakend
- how to create bootable usb stick
- how to setup linux to develop opengl game
- 事件传递机制和ViewDragHelper
- C++ 虚函数 多态的实现
- RyFit 智能体质分析仪 青春版与经典版之间的区别
- javaScript中parseFloat()结果精度不一致的问题
- js闭包