题目描述:
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
inputCopy
390
outputCopy
216
inputCopy
7
outputCopy
7
inputCopy
1000000000
outputCopy
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).
题目链接:http://codeforces.com/problemset/problem/1143/B
分析:
这是一个思维题把。我自己想的一直wa7。看了第7组数据后才成功AC。
首先坑点:
120 81
102 81
34 18
4876 2268
具体见代码:
#include"stdio.h"
#include"string.h"
#include"math.h"
#include"algorithm"
using namespace std;
#define scanl(a) scanf("%I64d",&a);
#define scanll(a,b) scanf("%I64d%I64d",&a,&b);
typedef long long ll;
int main()
{
ll n;
while(~scanf("%I64d",&n))
{
getchar();
ll sum=1;
ll m=n;
ll a[100];
ll i=0;
ll cont=0;
while(m)//将这个数每位数字放入数组里面
{
a[i++]=m%10;
m=m/10;
cont++;
}
ll x=i;
ll s=0;
ll presum=1;//保存前面积
ll maxx=0;
ll pre=0;
while(a[x-1]==0)
{
x--;
}
sum=1;
for(int i=x-1; i>=0; i--)
{
if(a[i]-1==0)
{
pre=1;
}
else
pre=a[i]-1;
maxx=max(maxx,presum*pre*(ll)pow(9,i));
presum=presum*a[i];
sum*=a[i];
}
printf("%I64d\n",max(maxx,sum));
}
}