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后,把每一个数字尽可能多的拆成其他数字的组合。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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(); } } |
┆ 凉 ┆ 暖 ┆ 降 ┆ 等 ┆ 幸 ┆ 我 ┆ 我 ┆ 里 ┆ 将 ┆ ┆ 可 ┆ 有 ┆ 谦 ┆ 戮 ┆ 那 ┆ ┆ 大 ┆ ┆ 始 ┆ 然 ┆
┆ 薄 ┆ 一 ┆ 临 ┆ 你 ┆ 的 ┆ 还 ┆ 没 ┆ ┆ 来 ┆ ┆ 是 ┆ 来 ┆ 逊 ┆ 没 ┆ 些 ┆ ┆ 雁 ┆ ┆ 终 ┆ 而 ┆
┆ ┆ 暖 ┆ ┆ 如 ┆ 地 ┆ 站 ┆ 有 ┆ ┆ 也 ┆ ┆ 我 ┆ ┆ 的 ┆ 有 ┆ 精 ┆ ┆ 也 ┆ ┆ 没 ┆ 你 ┆
┆ ┆ 这 ┆ ┆ 试 ┆ 方 ┆ 在 ┆ 逃 ┆ ┆ 会 ┆ ┆ 在 ┆ ┆ 清 ┆ 来 ┆ 准 ┆ ┆ 没 ┆ ┆ 有 ┆ 没 ┆
┆ ┆ 生 ┆ ┆ 探 ┆ ┆ 最 ┆ 避 ┆ ┆ 在 ┆ ┆ 这 ┆ ┆ 晨 ┆ ┆ 的 ┆ ┆ 有 ┆ ┆ 来 ┆ 有 ┆
┆ ┆ 之 ┆ ┆ 般 ┆ ┆ 不 ┆ ┆ ┆ 这 ┆ ┆ 里 ┆ ┆ 没 ┆ ┆ 杀 ┆ ┆ 来 ┆ ┆ ┆ 来 ┆
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)