URAL 1014 Product Of Digits

  • 注意特判 0 1
  • 分解因子 从9到2
 1 import java.util.Scanner;
 2
 3 public class P1014
 4 {
 5     public static void main(String args[])
 6     {
 7         try (Scanner cin = new Scanner(System.in))
 8         {
 9             while (cin.hasNext())
10             {
11                 int n = cin.nextInt();
12                 StringBuilder builder = new StringBuilder();
13                 if (n == 0)
14                     System.out.println(10);
15                 else if (n == 1)
16                     System.out.println(1);
17                 else
18                 {
19                     for (int k = 9; k >= 2; k--)
20                         while (n % k == 0)
21                         {
22                             n /= k;
23                             builder.append(k);
24                         }
25                     if (n == 1)
26                         System.out.println(builder.reverse());
27                     else
28                         System.out.println(-1);
29                 }
30             }
31         }
32     }
33 }
时间: 2024-10-28 14:38:09

URAL 1014 Product Of Digits的相关文章

10 URAL 1014 Product of Digits

找一个最小的正整数Q,Q的各个位置上的数字乘积等于N. 每一位只能是2-9,0和1没有用,那么能用尽量大的数就先用大的数,这样保证Q的位数最少, 从9枚举到2,如果W的因子只有9-2,那么有解,在把这些因子从小到大输出即可,否则无解. 注意要特判0和1,因为要求最小正整数,所以0的时候答案应该是10. #include<cstdio> int main() { int i,n,cnt,ans[40]; while(~scanf("%d",&n)) { if(n==0

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 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;

993 - Product of digits

题目:993 - Product of digits 题目大意:将一个数通过因式分解变成另一个由因子组成的最小的数. 解题思路:因为要组成数,所以因子只需要从 2 - 9,又因为需要最小的数,所以因式分解从最大的因子开始,这样位数最少,最后输出从最小的因子输出,保证最小.1的话需要特判,因为所有的数都有因子1. 代码: #include <stdio.h> #include <string.h> const int N = 10; int c[N]; int n; bool fac

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

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 of its digits. Example 1: Input: n = 234 Output: 15 Explanation: Product of digits = 2 * 3 * 4 = 24 Sum of digits = 2 + 3 + 4 = 9 Result = 24 - 9 = 15 Exam

1.3.3 Prime Cryptarithm

The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set of N digits into the positions marked with *. If the set of prime digits {2,3,5,7} is selected, the cryptarithm is called a PRIME CRY

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

优质题表(机密版)

转载请注明出处:http://www.cnblogs.com/dashuzhilin/p/4556803.html 思维题: poj 1528 poj 1597 poj 2538 poj 2608 poj 2612 poj 2361 poj 2339 poj 2664 uva 10894 uva 10921   uva 10922   uva 10929 uva 10931   uva 10800   uva 10878 uva 10976   uva 10323   uva 201 poj 2