UVa 993: Product of digits
这道题很简单。先将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 (0
N
109) .
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的更多相关文章
- uva 993 Product of digits (贪心 + 分解因子)
Product of digits For a given non-negative integer number N , find the minimal natural Q such tha ...
- UVa 10106 Product
高精度乘法问题,WA了两次是因为没有考虑结果为0的情况. Product The Problem The problem is to multiply two integers X, Y. (0& ...
- UVa 10106 Product 【大数相乘】WA
虽然是错的代码,但是还是想贴出来,最开始WA发现是没有考虑到乘积为0的情况,后来把a*0,0*a,a*0---0(若干个0),0--0(若干个0)*a都考虑进去了:可是还是WA,实在不懂先留在这儿. ...
- UVA 10106 Product (大数相乘)
Product The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The ...
- (高精度运算4.7.21)UVA 10106 Product(大数乘法)
package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class UVA_10106 ...
- UVA 12532-Interval Product(BIT)
题意: 给n个数字的序列,C v p 把v位上的数字置成p , S l r代表求区间[l,r]各数字相乘得数的符号,对于每个S输出所得符号(’+‘,’-‘,’0‘) 分析: 开两个数组表示区间负数的个 ...
- (贪心5.2.6)URAL 1014 Product of Digits(利用数据有序化进行贪心选择)
/* * URAL_1014.cpp * * Created on: 2013年10月11日 * Author: Administrator */ #include <iostream> ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- 【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 ...
随机推荐
- 如何在win7上面安装python的包
最近在win7上面搞python,然后写的一些代码涉及到了对Excel的读写.所以需要用到包xlrd xlwt xlutils. 但问题是这些包import后显示的是找不到.错误提示是:Import ...
- WebApi学习总结系列第四篇(路由系统)
由于工作的原因,断断续续终于看完了<ASP.NET Web API 2 框架揭秘>第二章关于WebApi的路由系统的知识. 路由系统是请求消息进入Asp.net WebApi的第一道屏障, ...
- C# Dispose Finalize
比较值得参考的文档:http://www.jb51.net/article/37214.htm. .NET 的内存管理过程: 托管堆假设内存无限大,线性连续分配内存: 实际内存不够使用时,遍历托管堆对 ...
- spoj 4487. Can you answer these queries VI (gss6) splay 常数优化
4487. Can you answer these queries VI Problem code: GSS6 Given a sequence A of N (N <= 100000) in ...
- 深入了解一下PYTHON中关于SOCKETSERVER的模块-D
在LINUX上以FORK的方式使用SIMPLEHTTPSERVER. 记得哟,THREADING能在WIN和LINUX上实现,但FORK,不支持WIN的. 除了SIMPLE之外,还有CGI,就不示例啦 ...
- 【POJ2478】Farey Seque
题意: 就是求2~n的所有欧拉函数值的和,这里就用到了快速求欧拉函数的方法.(不能暴力求了,不然必定TLE啊) 说说欧拉筛法,感觉十分机智啊~~ 对于上述代码的几个问题: 1.问:为什么i%prime ...
- 关于使用Element.getNodeValue()返回NULL的问题
使用DOM方法解析XML数据, 比如这个xml:<root> <name>admin</name></root> 在程序中读取 Document do ...
- 纯CSS打造可折叠树状菜单
1:Html代码 <li> <label for="subsubfolder1">下级</label> <input id="s ...
- PHP,JAVA,JAVASCRIPT的正则表达式里的反斜杠\的不通之处
我的博客:www.while0.com 首先,java和javascript强制字符串输出\必须用\转义,所以要输出一个\,java和javascript都要两个\: java代码: String s ...
- [LeetCode#163] Missing Ranges
Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...