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 }
  • 相关阅读:
    万豪项目总结
    解决jquery animate({scrollTop$pos},500)与$(window).scroll方法冲突的问题
    一波水文来袭-让我们一起谈谈闭包【原创】
    JS模块化规范AMD之RequireJS
    JS模块化规范CMD之SeaJS
    邂逅Sass和Compass之Compass篇
    邂逅Sass和Compass之Sass篇
    idea 修改SVN账户信息
    idea 创建/加载 maven项目速度较慢
    gitlab新建分支,idea中无法找到
  • 原文地址:https://www.cnblogs.com/yewanting/p/10727219.html
Copyright © 2011-2022 走看看