[题解] Codeforces Round #549 (Div. 2) B. Nirvana

Codeforces Round #549 (Div. 2) B. Nirvana

 

[题目描述]

B. Nirvana

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

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≤n≤2⋅109).

 

Output

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

Examples

input

390

output

216

input

7

output

7

input

1000000000

output

387420489

Note

In the first example the maximum product is achieved for 389389 (the product of digits is 3⋅8⋅9=2163⋅8⋅9=216).

In the second example the maximum product is achieved for 77 (the product of digits is 77).

In the third example the maximum product is achieved for 999999999999999999 (the product of digits is 99=38742048999=387420489).

    [解法]

    尽量将每一位变为9,或者向前退位来变成9,并且前一位减1.

    [代码(AC)]

    

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <iostream>
 5 #include <iostream>
 6 #include <iostream>
 7 #include <iostream>
 8 #include <iostream>
 9 using namespace std;
10 int ans(int n){
11     if(n==0)return 1;
12     if(n<10)return n;
13     return max(ans(n/10)*(n%10),ans(n/10-1)*9);
14 }
15 int main() 
16 {
17     int n;
18     scanf("%d", &n);
19     printf("%d", ans(n));
20 } 

 

 

2019-04-04 18:05:50

 

转载于:https://www.cnblogs.com/zjd-ac/p/10656330.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值