zoukankan      html  css  js  c++  java
  • Codeforces Round #549 (Div. 2)B. Nirvana

    B. Nirvana
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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 11 to nn.

    Input

    The only input line contains the integer nn (1n21091≤n≤2⋅109).

    Output

    Print the maximum product of digits among all integers from 11 to nn.

    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 389=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).

    解题思路:这道题就是给你一个n数,问你1到n之间哪个数能使各个位数相乘最大;

    首先我们想到尽量将每一位变为9,然后每次都向前借一位来减。

    注意当K为0时,表示前面的数字没了,所以应该返回1。

    代码如下:

     1 #include<iostream>
     2 #include<cmath>
     3 using namespace std;
     4 int n ;
     5 int cj(int k)
     6 {
     7     if(k==0)
     8     return 1;
     9     if(k<10)
    10     {
    11         return k;
    12     }
    13     
    14     return max(cj(k/10)*(k%10),cj(k/10-1)*9);
    15     
    16 }
    17 int main()
    18 {
    19     cin>>n;
    20     cout<<cj(n);
    21     return 0 ;
    22 }
  • 相关阅读:
    5.11实例应用
    VS2015调试
    4.4空间平滑
    4.3图像噪声
    4.2 傅里叶变换
    4.1 图像采样
    4.5.实例应用
    关于split和merge出错问题解决
    Ansible用于网络设备管理 part 3 使用NAPALM成品库
    记办公室小机房停电
  • 原文地址:https://www.cnblogs.com/yewanting/p/10727219.html
Copyright © 2011-2022 走看看