XiaoHuang's Space
XiaoHuang's Space
XiaoHuang
May the force be with you.
『题解』Codeforces1143B Nirvana
Posted: Dec 11, 2019
Last Modified: Dec 13, 2019
This article was last modified days ago. The content of this post may be outdated!

Portal

Portal1: Codeforces

Portal2: Luogu

Description

Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater value of the product makes the nirvana deeper.

Help Kurt find the maximum possible product of digits among all integers from $1$ to $n$.

Input

The only input line contains the integer $n (1 \le n \le 2 \times 10 ^ 9)$.

Output

Print the maximum product of digits among all integers from $1$ to $n$.

Sample Input1

390

Sample Output1

216

Sample Input2

7

Sample Output2

7

Sample Input3

1000000000

Sample Output3

387420489

Solution

我们可以用递归,每次从右边少一位下去,进行递归,如果剩下的数是不为0的一位数,就返回这个数,否则返回1,然后枚举最后一位,选择直接用那一位的数或者退位。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

int n;
inline int f(int x) {
    if (x < 10) {//判断一位数的情况
        if (x == 0) return 1; else return x;
    }
    return max((x % 10) * f(x / 10), 9 * f(x / 10 - 1));//直接用那一位的数或者退位
}
int main() {
    scanf("%d", &n);
    printf("%d\n", f(n));
    return 0;
}
Article License: CC BY-NC-ND 4.0
Article Author: XiaoHuang
  1. 1. Portal
  2. 2. Description
  3. 3. Input
  4. 4. Output
  5. 5. Sample Input1
  6. 6. Sample Output1
  7. 7. Sample Input2
  8. 8. Sample Output2
  9. 9. Sample Input3
  10. 10. Sample Output3
  11. 11. Solution
  12. 12. Code
Newer Post
『题解』Codeforces1143C Queen
Older Post
『题解』Codeforces1143A The Doors
Buy me a beer?
-->
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×