这道题很简单。先将N用2,3,5,7(即10以内的素数)分解因数(需要先特殊判断N不为1),然后将可以合并的因数合并(如2*2合并成4,)这样求得的结果位数会减少,大小肯定会小一些。具体实现见代码。

我的解题代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std; int c[12];
int T;
int N; int main()
{
cin >> T;
while(T--)
{
cin >> N;
if(N==1) { cout << 1 << endl; continue; }
memset(c,0,sizeof(c));
while(N!=1) //分解N
{
if(N%2==0) { c[2]++; N/=2; }
else if(N%3==0) { c[3]++; N/=3; }
else if(N%5==0) { c[5]++; N/=5; }
else if(N%7==0) { c[7]++; N/=7; }
else break;
}
if(N!=1) { cout << -1 << endl; continue; }
while(1) //合并N的因子
{
if(c[2]>=3) { c[2]-=3; c[8]++; } //因子有三个2,合并为8
else if(c[2]>=2) { c[2]-=2; c[4]++; } //有两个2,合并为4
else if(c[3]>=2) { c[3]-=2; c[9]++; } //有两个3,合并为9
else if(c[2]>=1 && c[3]>=1) { c[2]--; c[3]--; c[6]++; } //有一个2和一个3,合并为6
else break;
}
for(int i=2; i<=9; i++)
{//输出结果
while(c[i])
{
cout << i;
c[i]--;
}
}
cout << endl;
}
return 0;
}

附上题目如下:

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

UVa 993: Product of digits的更多相关文章

  1. uva 993 Product of digits (贪心 + 分解因子)

      Product of digits  For a given non-negative integer number N , find the minimal natural Q such tha ...

  2. UVa 10106 Product

    高精度乘法问题,WA了两次是因为没有考虑结果为0的情况.  Product  The Problem The problem is to multiply two integers X, Y. (0& ...

  3. UVa 10106 Product 【大数相乘】WA

    虽然是错的代码,但是还是想贴出来,最开始WA发现是没有考虑到乘积为0的情况,后来把a*0,0*a,a*0---0(若干个0),0--0(若干个0)*a都考虑进去了:可是还是WA,实在不懂先留在这儿. ...

  4. UVA 10106 Product (大数相乘)

    Product The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The ...

  5. (高精度运算4.7.21)UVA 10106 Product(大数乘法)

    package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class UVA_10106 ...

  6. UVA 12532-Interval Product(BIT)

    题意: 给n个数字的序列,C v p 把v位上的数字置成p , S l r代表求区间[l,r]各数字相乘得数的符号,对于每个S输出所得符号(’+‘,’-‘,’0‘) 分析: 开两个数组表示区间负数的个 ...

  7. (贪心5.2.6)URAL 1014 Product of Digits(利用数据有序化进行贪心选择)

    /* * URAL_1014.cpp * * Created on: 2013年10月11日 * Author: Administrator */ #include <iostream> ...

  8. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  9. 【leetcode】1281. Subtract the Product and Sum of Digits of an Integer

    题目如下: Given an integer number n, return the difference between the product of its digits and the sum ...

随机推荐

  1. dotnet core 开发体验之Routing

    开始 回顾上一篇文章:dotnet core开发体验之开始MVC 里面体验了一把mvc,然后我们知道了aspnet mvc是靠Routing来驱动起来的,所以感觉需要研究一下Routing是什么鬼. ...

  2. 【python之旅】python的面向对象

    一.面向过程 VS 面向对象 1.编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,实现一个任务的 ...

  3. Day18 Django之路由系统、模板语言、Ajax、Model

    一.路由系统 1.创建Django项目 django-admin startproject day18 cd day18 python3 manage.py startapp app01 2.app0 ...

  4. ERROR 2003 (HY000): Can't connect to MySQL server

    http://blog.csdn.net/longxibendi/article/details/6363934 一.问题的提出 /usr/local/webserver/mysql/bin/mysq ...

  5. C# 下载资源

    //创建一个初始化请求对象 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://wwww.b ...

  6. Laravel框架——任务调度(cron)

    准备: 在服务的/var/spool/cron/root文件中添加代码 cd /var/spool/cron/root 添加以下代码 * * * * * phppath 项目路径/artisan sc ...

  7. ARC - MRC

    1. 选择工程 ---> build phases  --> .m中添加 -fno-objc-arc

  8. applicationContext.xml详解 spring+mybatis+struts

    今天给大家详细解释一项关于Spring的applicationContext.xml文件,这对于初学者来说,应该是很有帮助的, 以下是详解Spring的applicationContext.xml文件 ...

  9. nodejs注册为windows服务

    http://blog.csdn.net/puncha/article/details/9047311 http://www.oschina.net/question/12_18694 http:// ...

  10. 如何在DJANGO里,向有外键(一对多和多对多)的DB里插入数据?

    需要插入的数据表结构如下: class UserInfo(models.Model): user_id =models.AutoField(primary_key=True) user_name=mo ...