您的位置:首页 > 其它

Codeforces Round #292 (Div. 2) C. Drazil and Factorial(贪心)

2017-07-19 12:50 525 查看

传送门

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();
}
}

  

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: