zoukankan      html  css  js  c++  java
  • [题解] 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设置静态IP
    jenkins+findbugs
    CentOS 6.6 安装 Node.js
    未来物联网、人工智能无法迈过的技术是什么
    未来物联网、人工智能无法迈过的技术是什么
    spss-数据清洗-处理重复数据
    spss-数据清洗-处理重复数据
    大数据时代数据管理方式研究
    大数据时代数据管理方式研究
    Excel表格文本格式的数字和数字格式如何批量转换
  • 原文地址:https://www.cnblogs.com/zjd-ac/p/10656330.html
Copyright © 2011-2022 走看看