Codeforces Round #549 (Div. 2)B. Nirvana
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.
The only input line contains the integer nn (1≤n≤2⋅1091≤n≤2⋅109).
Print the maximum product of digits among all integers from 11 to nn.
390
216
7
7
1000000000
387420489
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).
解题思路:这道题就是给你一个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 }