18级暑假个人赛(二) Nirvana

本文探讨了一道算法题目,目标是在从1到n的所有整数中找到具有最大数字乘积的整数。通过逐步将数字的位数转换为9来寻找这个最大值,例如从390开始,通过转换得到389,然后299等,直至找到最大乘积。

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

.

Sample Input
Input

390

Output

216

Input

7

Output

7

Input

1000000000

Output

387420489

Hint

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).

题目思想并不难,但如果想错了方向就没了,比赛的时候就想着直接找规律输出答案,思考错了方向。
思路:每次更改后面一位变为9,寻找最大值,比如390->389->299……
代码:(注意要用G++17交,不然有可能会WA 7)

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define mem(a, b) memset(a, b, sizeof a)
#define IN freopen("in.txt", "r", stdin)
#define DEBUG(a) cout << (a) << endl
typedef long long ll;
const int INF = 0x3f3f3f3f;
int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
int main()
{
     int i,n,m,j,k=1,l,max1=1;
    scanf("%d",&n);
    m=n;max1=1;k=1;
    while(m>0)
    {
        j=m%10;
        m=m/10;
        max1=max1*j;
    }
    for(i=1;;i++)
    {
        m=n; int ans=1;
        l=pow(10,i);
        m=(m/l-1)*l;
        if(m<0)break;
        m=m+l-1;
        while(m>0)
        {
        j=m%10;
        m=m/10;
        ans=ans*j;
        }
        max1=max(max1,ans);
    }
    printf("%d\n",max1);
    return 0;
}

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值