Codeforces Round #292 (Div. 2) C. Drazil and Factorial(贪心)
Description
Drazil is playing a math game with Varda.
Let's define
for positive integer x as a product of factorials of its digits. For example,
.
First, they choose a decimal number a consisting of n digits that contains at least one digit larger than 1. This number may possibly start with leading zeroes. Then they should find maximum positive number x satisfying following two conditions:
1. x doesn't contain neither digit 0 nor digit 1.
2.
=
.
Help friends find such number.
Input
The first line contains an integer n (1 ≤ n ≤ 15) — the number of digits in a.
The second line contains n digits of a. There is at least one digit in a that is larger than 1. Number a may possibly contain leading zeroes.
Output
Output a maximum possible integer satisfying the conditions above. There should be no zeroes and ones in this number decimal representation.
Sample Input
4
1234
3
555
Sample Output
33222
555
Note
In the first case, 
思路
题解:
处理前导0后,把每一个数字尽可能多的拆成其他数字的组合。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 20;
char s[maxn];
int a[maxn<<2];
bool cmp(int x,int y)
{
return x > y;
}
int main()
{
int n;
scanf("%d",&n);
scanf("%s",s);
int index = 0,tot = 0;
while (s[index] == '0') index++;
for (int i = index;i <= n;i++)
{
if (s[i] == '0') continue;
if (s[i] == '1') continue;
if (s[i] == '2') a[tot++] = 2;
if (s[i] == '3') a[tot++] = 3;
if (s[i] == '4') a[tot++] = 3,a[tot++] = 2,a[tot++] = 2;
if (s[i] == '5') a[tot++] = 5;
if (s[i] == '6') a[tot++] = 5,a[tot++] = 3;
if (s[i] == '7') a[tot++] = 7;
if (s[i] == '8') a[tot++] = 7,a[tot++] = 2,a[tot++] = 2,a[tot++] = 2;
if (s[i] == '9') a[tot++] = 2,a[tot++] = 3,a[tot++] = 3,a[tot++] = 7;
}
sort(a,a+tot,cmp);
for (int i = 0;i < tot;i++) printf("%d",a[i]);
printf("\n");
return 0;
}
import java.util.Arrays;
import java.util.Scanner;
import static java.lang.System.out;
public class DrazilAndFactorial {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.nextLine();
String s = scan.nextLine();
s = s.replaceAll("0", "");
s = s.replaceAll("1", "");
s = s.replaceAll("4", "223");
s = s.replaceAll("6", "53");
s = s.replaceAll("8", "7222");
s = s.replaceAll("9", "7332");
char[] c = s.toCharArray();
Arrays.sort(c);
for (int i = 0;i < c.length;i++) {
out.print(c[c.length-1-i]);
}
out.println();
}
}
┆ 凉 ┆ 暖 ┆ 降 ┆ 等 ┆ 幸 ┆ 我 ┆ 我 ┆ 里 ┆ 将 ┆ ┆ 可 ┆ 有 ┆ 谦 ┆ 戮 ┆ 那 ┆ ┆ 大 ┆ ┆ 始 ┆ 然 ┆
┆ 薄 ┆ 一 ┆ 临 ┆ 你 ┆ 的 ┆ 还 ┆ 没 ┆ ┆ 来 ┆ ┆ 是 ┆ 来 ┆ 逊 ┆ 没 ┆ 些 ┆ ┆ 雁 ┆ ┆ 终 ┆ 而 ┆
┆ ┆ 暖 ┆ ┆ 如 ┆ 地 ┆ 站 ┆ 有 ┆ ┆ 也 ┆ ┆ 我 ┆ ┆ 的 ┆ 有 ┆ 精 ┆ ┆ 也 ┆ ┆ 没 ┆ 你 ┆
┆ ┆ 这 ┆ ┆ 试 ┆ 方 ┆ 在 ┆ 逃 ┆ ┆ 会 ┆ ┆ 在 ┆ ┆ 清 ┆ 来 ┆ 准 ┆ ┆ 没 ┆ ┆ 有 ┆ 没 ┆
┆ ┆ 生 ┆ ┆ 探 ┆ ┆ 最 ┆ 避 ┆ ┆ 在 ┆ ┆ 这 ┆ ┆ 晨 ┆ ┆ 的 ┆ ┆ 有 ┆ ┆ 来 ┆ 有 ┆
┆ ┆ 之 ┆ ┆ 般 ┆ ┆ 不 ┆ ┆ ┆ 这 ┆ ┆ 里 ┆ ┆ 没 ┆ ┆ 杀 ┆ ┆ 来 ┆ ┆ ┆ 来 ┆

浙公网安备 33010602011771号