求解一道编程题 URAL - 1014

勤勤勤能补拙 2017-04-16 08:42:09
连接 https://vjudge.net/contest/158881#problem/A
昨天做题,第一道题就遇见瓶颈,可我又不想搜答案,这是我的代码,希望大家帮我看一下有什么问题,我还是小白,希望大家多多指点。
题目内容:

A - Product of Digits URAL - 1014
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 ≤ 10 9).
Output
Your program should print to the output the only number Q. If such a number does not exist print −1.
Example
input output

10 25

这是我的代码,一直wrong answer
#include<stdio.h>
#include<math.h>
#include<string>
#define MAX 2100000000
typedef long long LL;
LL weishu=MAX; //所求最小数的位数
LL m=MAX;//所求最小数
LL isPrime(LL n) //判断n是否为素数
{
LL i;
if(n==1)return 0;
if(n==2)return 1;
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)return 0;
}
return 1;
}
void dfs(LL n,LL ans,LL num)
{
if(n>=11&&isPrime(n)){//大于等于11且是质数,则这种方案不成立
return ;
}
if(n<=9){ //如果数字小于9 就可以取了,不用再深度搜索
if((ans+1<weishu)||(ans+1==weishu)&&(num*10+n<m)){ //如果当前位数小于weishu或者,位数相等但是值要比m小,都将替代最优解m,weishu
m=num*10+n;
weishu=ans+1;
}
}else{
LL i;
for(i=2;i<10&&i<=sqrt(n);i++) //遍历,查找n的每一个因子
{
if(n%i==0){ //一旦发现有因子,对n/i进行深度搜索
dfs(n/i,ans+1,num*10+i);
}
}
}
}
int main()
{
LL n;
scanf("%lld",&n);
dfs(n,0,0);
if(m==MAX){
printf("-1\n");
}else{
printf("%I64d\n",m);
}
return 0;
}


...全文
144 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
FancyMouse 2017-04-17
  • 打赏
  • 举报
回复
minimal positive integer number Q
FancyMouse 2017-04-17
  • 打赏
  • 举报
回复
看FAQ。64位要用标准的%lld不是老vc的I64d
勤勤勤能补拙 2017-04-17
  • 打赏
  • 举报
回复
明白,可是补齐了这种情况还是wrong answer,想了很久不明白错在哪里
FancyMouse 2017-04-16
  • 打赏
  • 举报
回复
N=0应该输出10吧
勤勤勤能补拙 2017-04-16
  • 打赏
  • 举报
回复
也是wrong answer,请问为什么0会输出10? 0的成绩不也是0吗

64,691

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧