Gym 100886G Maximum Product

the description of the problem

Find the number from the range [a, b] which has the maximum product of the digits.

Input
The first line contains two positive integers a and b (1 ≤ a ≤ b ≤ 1018): the left and the right ends of the range.

Output
Print the number with the maximum product of the digits from the range [a, b]. If there are several possible answers, print any one of them.

Sample Input
Input
1 10
Output
9
Input
51 62
Output
59
FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
All Copyright Reserved ©2010-2014 HUST ACM/ICPC TEAM
Anything about the OJ, please ask in the forum, or contact author:Isun
Server Time: 2016-03-01 12:24:23

题目大意:求一个给定区间内的每个数位最大的乘积,求这个数字。
解题思路:基于了一种贪心策略:按照小于b的不同位数,求每种情况的最大值。具体为:取temp_b = b 不断按位数,进行求值。
if(temp_b%10 == 9) ,num. push_back(9),temp_b/=10;
else if(temp_b>=10),num.push_back(9),temp_b-=1,temp_b/=10;
else num.push_back(temp_b%10),temp_b/=10;
具体看代码。

//
// Created by 王若璇 on 16/2/29.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
LL ten[30];
LL getProject(LL x){
    LL tmp = 1;
    while (x){
        tmp*=(x%10);
        x/=10;
    }
    return tmp;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    ten[0]=1;
    for(int i = 1;i<=29;i++){
        ten[i] = ten[i-1]*10;
    }
    LL a,b;
    while (cin>>a>>b){
        LL MAX = getProject(b);
        LL re = b;
        for(int pos = 1;pos<18;pos++){
            LL tmp_b= b;
          //  LL re = b;
            vector<int> tmpre;
            tmpre.clear();
            for(int i =1;i<=pos&&tmp_b;i++){
                if(tmp_b%10==9){
                    tmp_b/=10;
                    tmpre.push_back(9);
                } else if(tmp_b>=10){
                    tmp_b/=10;
                    tmp_b--;
                    tmpre.push_back(9);
                } else{
                    tmpre.push_back(tmp_b%10);
                    tmp_b/=10;
                }
            }
            LL cur = 0;
            for(int i = tmpre.size()-1;i>=0;i--){
                cur = cur*10+tmpre[i];
            }
            cur = tmp_b*ten[tmpre.size()]+cur;
            if(cur<a){
                continue;
            }
            if(getProject(cur)>MAX){
                MAX = getProject(cur);
                re = cur;
            }
        }
        cout<<re<<endl;
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值