这道题很简单。先将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. npoi批量

    npoi批量导入实现及相关技巧 批量导入功能对于大部分后台系统来说都是不可或缺的一部分,常见的场景-基础数据的录入(部门,用户),用批量导入方便快捷.最近项目需要用到批量导入,决定花点时间写套比较通用 ...

  2. 学习Swift -- 析构过程

    析构过程 析构器只适用于类类型,当一个类的实例被释放之前,析构器会被立即调用.析构器用关键字deinit来标示,类似于构造器要用init来标示. 原理 Swift 会自动释放不再需要的实例以释放资源, ...

  3. 什么是image crop?

    一直对image crop很困惑,总算是看到了一篇描述较为简洁的说明:图像crop就是指从图像中移除不需要的信息,只保留需要的部分

  4. 判断是否为ie(包含ie11)

    if (!!window.ActiveXObject || "ActiveXObject" in window) { alert("IsIE"); }

  5. bzoj 1406: [AHOI2007]密码箱 二次剩餘

    1406: [AHOI2007]密码箱 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 701  Solved: 396[Submit][Status] D ...

  6. 15个你不知道的杀手级Google Chrome功能

    之前写过一篇我为什么使用Google Chrome浏览器,算是在使用了一段时间的Google Chrome后的一点粗浅认识,最近读到一篇文章15 Killer Google Chrome Featur ...

  7. 加快AndroidStudio运行速度的方法

    之前用过其他人加速AndroidStudio构建速度的方法,确实在编译时有一定的效果 但是在实际使用中,随着项目越来越大,AndroidStudio有时还是会卡死,或者直接黑屏,我的笔记本是8g内存 ...

  8. Android开源项目发现--- 工具类数据库ORM篇(持续更新)

    orm的db工具类,简化建表.查询.更新.插入.事务.索引的操作 1. greenDAO Android Sqlite orm的db工具类 项目地址:https://github.com/greenr ...

  9. Android AlertDialog更改标题颜色,字体等

    更改AlertDialog标题的方法google目前没有提供,只能通过其他办法 一种办法是:首先在源代码中找到有个叫AlertController的类,这个类就是AlertDialog的实现类,是没有 ...

  10. Eclipse配置Git

    一.Eclipse上安装GIT插件EGit Eclipse的版本eclipse-java-helios-SR2-win32.zip(在Eclipse3.3版本找不到对应的 EGit插件,无法安装) E ...