uva--993Product of digits

水题一枚,如果n能分解成2--9的因子相乘的形式的话肯定是可以的,如果不行那么就直接输出-1;

那么当能分解的时候,为了能使得答案的值最小,我们选择先分解大的因子;也就是按9--2的次序进行分解。

分解后直接按因子从小到大输出就是答案了。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int main()
{

      int i,j,k,t,ans[50];
      long long n;
      scanf("%d",&t);
      while(t--)
      {
             k=0;
             scanf("%lld",&n);
             if(n==1)
             {
                    printf("1\n");
                    continue;
             }
             for(i=9;i>=2;i--)
                        while(n%i==0)
                        {
                                ans[k++]=i;
                                 n=n/i;
                        }
             if(n>10)
             {
                    printf("-1\n");
                    continue;
             }
             for(i=k-1;i>=0;i--)
                 printf("%d",ans[i]);
            printf("\n");
      }
     return 0;
}
时间: 2024-12-21 16:54:51

uva--993Product of digits的相关文章

UVA 1341 - Different Digits(数论)

UVA 1341 - Different Digits 题目链接 题意:给定一个正整数n,求一个kn使得kn上用的数字最少,如果相同,则输出值最小的 思路: 首先利用鸽笼原理证明最多需要2个数字去组成 设一个数字k,组成k,kk,kkk,kkkk... %n之后余数必然在0 - (n - 1)之间,所以必然能选出两个余数相等的数字相减为0,这个数字就是由0和k组成的. 因此只要考虑一个数字和两个数字的情况,去bfs,记忆化余数,因为余数重复必然形成周期了 代码: #include <stdio.

uva 1341 - Different Digits(数论+bfs)

题目链接:uva 1341 - Different Digits 题目大意:给定一个数字n,要求求一个数字m,m可以整除n,并且尽量组成的数字种类(0~9)尽量少,在种类相同的情况下数值尽量小. 解题思路:可以证明两种数字肯定可以组成m,假设有数字k,一种数字可以有k,kk,kkk,kkkk,-整除n剩的数一定在0~n-1之间,所以肯定存在两个由k数字组成的数字同模,那么这两个数相减所得到的数即使kkk00000,两种数字.于是肯定了范围,枚举数字,然后用bfs获取答案,维护最小值即可. #in

UVA 10061 How many zero&#39;s and how many digits ? (m进制,阶乘位数,阶乘后缀0)

题意:给出两个数字a和b,求a的阶乘转换成b进制后,输出(1)后缀中有多少个连续的0? (2)有多少位? 思路:逐个问题解决. 设a!=k.  k暂时不用直接转成b进制. (1)阶乘后缀0问题.先看这个十进制后缀0的例子:http://www.cnblogs.com/xcw0754/p/4604473.html 解法差不多,稍变化. 首先将b分解成若干质数(比如8={2*2*2})保存在一个集合A中(注意自然数的质数分解是唯一的),只要有一个序列A就能构成一个0,因为满b就进位,b可以表示成10

UVA - 10061 How many zero&#39;s and how many digits ?

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19099 给定一些点,求构成三角形最大面积,并且这个三角形不能不能包含其他点(在边上也不行). 因为数据范围很小,所以直接枚举就好,并且还把面积公式告诉了我们,判断点在三角形内的方法是,这个点与其他顶点的面积之和是否等于这个三角形的面积. #include<cstdio> #include<cmath> struct point { char z; int x,

uva 993 Product of digits (分解因子)

uva 993 Product of digits 题目大意:给定一个数N,要求出一个数,使得这个数的每一位相乘会等于N,且这个数的值最小. 解题思路:简单的分解因子.要注意的地方有两点:1) 因子从9开始向2遍历,可以保证位数最小: 2)当N等于1是最好特殊处理. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std;

UVA - 10061 How many zero&amp;#39;s and how many digits ?

n!=x*b^y, 当x为正整数时,最大的y就是n!末尾0的个数了, 把n,b分别拆成素因子相乘的形式: 比如, n=5,b=16 n=5,b=2^4, 非常明显,末尾0的个数为0 10进制时,n!=a*10^x b进制时,n!=c*b^y 非常明显,n!的位数就是最大的x+1 这里计算我用了log,精度设置为1e-9 #include<iostream> #include<cstdio> #include<vector> #include<cstring>

UVa 993 - Product of digits

题目:给你一个整数n,求一个数m,使得m的每个位数的乘积是n,求最小的m. 分析:贪心.直接从9到2枚举所有的因数,统计即可.如果还有大于9的素数这输出-1. 说明:今天Timus发邮件,说我的这个题目,在那边的解错了╮(╯▽╰)╭. #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int save[10]; int main() { int n,m,now; w

UVA - 993 - Product of digits (简单贪心)

993 Product of digits For a given non-negative integer number N, ?nd the minimal natural Q such that the product of all digits of Q is equal N. Input The ?rst line of input contains one positive integer number, which is the number of data sets. Each

uva 575 Skew Binary(数论)

uva 575 Skew Binary When a number is expressed in decimal, the k-th digit represents a multiple of 10k. (Digits are numbered from right to left, where the least significant digit is number 0.) For example, When a number is expressed in binary, the k-

UVA 10746 Crime Wave – The Sequel(费用流)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1687 ProblemG CrimeWave – The Sequel Input: StandardInput Output: StandardOutput TimeLimit: 2 Seconds n bankshave been robbed this fine day. m (grea