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