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 389
(the product of digits is 3⋅8⋅9=216
).
In the second example the maximum product is achieved for 7
(the product of digits is 7
).
In the third example the maximum product is achieved for 999999999
(the product of digits is 99=387420489)
题意:找出从1到N的数中每个数字乘积最大的值
思路:把每一位变成9,下一位- -,每次都比较一下看看是不是最大值
#include<iostream>
using namespace std;
long long int N;
int num[20];
int ans=1;
int getresult(int num[],int len){
int mul=1;
int tmp=len;
while(num[tmp]==0){
tmp--;
}
for(int i=tmp;i>=0;i--){
mul*=num[i];
}
return mul;
}
int main(){
cin>>N;
int tmp=0;
while(N){
int a=N%10;
num[tmp++]=a;
N/=10;
ans*=a;
}
for(int i=0;i<tmp-1;i++){
if(num[i]==9)continue;
num[i]=9;
num[i+1]--;
ans=max(ans,getresult(num,tmp));
}
cout<<ans<<endl;
return 0;
}