• [题解] 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

  • 相关阅读:
    测试流程之需求评审
    如何编写测试计划
    一定要知道的,那些Linux操作命令
    线上bug分析
    做一个靠谱的软件测试人员
    测试方向
    怎样才能提交一个让开发人员拍手叫好的bug单
    软件测试职业发展
    MongoDB的启动流程
    百度语音
  • 原文地址:https://www.cnblogs.com/zjd-ac/p/10656330.html
Copyright © 2020-2023  润新知