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 
#include 
#include 
#include 
#include 
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(curcontinue;
            }
            if(getProject(cur)>MAX){
                MAX = getProject(cur);
                re = cur;
            }
        }
        cout<return 0;
}

你可能感兴趣的:(acm集训)