搜索水题
来源:互联网 发布:永久免费域名 编辑:程序博客网 时间:2024/06/07 20:54
T - dfs-easy
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %lluDescription
Product of digits
For a given non-negative integer number N , find the minimal natural Q such that the product of all digits of Q is equal N .
Input
The first line of input contains one positive integer number, which is the number of data sets. Each subsequent line contains one data set which consists of one non-negative integer number N (0N109) .
Output
For each data set, write one line containing the corresponding natural number Q or `-1' if Q does not exist.
Sample Input
3 1 10 123456789
Sample Output
1 25 -1
题意挺难理解滴:给你一个数n,让你找出一个最小的数,满足该数的每一位的乘积为n.
思路:每位最多有1,2,3,4,5,6,7,8,9.这9中可能。数最大为pow(10,9)。所以不用担心,搜索会超时。枚举每一位,从高位到地位,利用贪心的思想:】
#include<iostream>#include<algorithm>#include<cstdio>#include<string.h>using namespace std;int ans[20];int num[10]= {2,3,4,5,6,7,8,9};int flag;int step;void DFS(int tem,int an){ if(tem==1) { flag=2; return ; } if(an==0&&tem%2!=0&&tem!=1) { flag=1; return; } for(int i=an; i>=0; i--) { if(tem%num[i]==0) { ans[step++]=num[i]; DFS(tem/num[i],i); if(flag==1||flag==2) return ; } if(i==0&&tem%2!=0&&tem!=1) { flag=1; return; } }}int main(){ int T; int t; scanf("%d",&T); while(T--) { step=flag=0; memset(ans,0,sizeof(ans)); scanf("%d",&t); if(t==1) { printf("1\n"); continue; } DFS(t,7); if(flag==1) printf("-1"); else for(int i=step-1; i>=0; i--) printf("%d",ans[i]); printf("\n"); } return 0;}
- 搜索水题
- 搜索水题
- hdu1226 搜索水题
- Hduoj2952【搜索水题】
- hdu 1584 搜索水题
- 搜索水题 (排列组合)
- 搜索题
- 搜索题
- 【搜索】两道搜索题
- hdu1104 搜索水题《记录路径》
- hdu1429 状态压缩搜索水题
- hdu4845 状态压缩搜索水题
- hdu2102 (dfs)搜索水题
- 畅通工程(搜索水题)
- hdu1198 不一样的搜索水题
- POJ-1979(搜索水题)
- HDOJ1208 记忆化搜索水题
- HDU 1241 Oil Deposits (水水的一道搜索题,我的搜索由此启程)
- javascript学习(6)location对象
- github生成SSH公钥
- Sublime Text 2 编辑器实用技巧
- javascript学习(7)Dom
- Heap与stack的差别。
- 搜索水题
- 各种排序方法级复杂度总结
- 隐式类型转换与转换操作符operatorT
- C++输出N位水仙花数
- 菜鸟程序员成就达成
- 避免使用宏
- H264--Intel硬编码
- 类常量
- C++定义类对圆做运算