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 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));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值