Codeforces Round #549 div2 1143-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
input:
390
output:
216
input:
7
output:
7
input:
1000000000
output:
387420489
一开始看到这道题的时候还是有点伤脑筋的,一直没想出来,想了十多分钟后想到办法后AC
代码使用字符串模拟数字求得结果,getvalue()函数是求得结果,calc()函数是递归函数,用于求得最大值
这个采用的递归搜索,设定结果函数为calc(),字符串长度为n,则calc返回以下其中一个最大解:
- getvalue( {s[0], s[1], ..., s[n-1]} )
- getvalue( {s[0]-1, 9, ..., 9} ) //连续n-1个9
- getvalue( {9, 9, ..., 9} ) //连续n-1个9
- s[0] * calc( {s[1], s[2], ..., s[n-1]} )
代码如下:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long ll;
ll getvalue(string s) {
ll sum = 1;
for (int i = 0; i < s.size(); i++) {
sum *= s[i] - '0';
}
return sum;
}
ll calc(string s) {
string q1 = s;
q1[0]--;
for (int i = 1; i < q1.size(); i++)q1[i] = '9';
return max({
getvalue(s),
getvalue(q1),
getvalue(q1.substr(1)),
(s.size() > 1) ? ((ll)(s[0] - '0')*calc(s.substr(1))) : 0
});
}
int main() {
string s;
cin >> s;
cout << calc(s) << endl;
}
Codeforces Round #549 div2 1143-B Nirvana 题解的更多相关文章
- [题解] Codeforces Round #549 (Div. 2) B. Nirvana
Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...
- Codeforces Round #549 (Div. 2)B. Nirvana
B. Nirvana time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- codeforces round 472(DIV2)D Riverside Curio题解(思维题)
题目传送门:http://codeforces.com/contest/957/problem/D 题意大致是这样的:有一个水池,每天都有一个水位(一个整数).每天都会在这一天的水位上划线(如果这个水 ...
- Codeforces Round #549 (Div. 1)
今天试图用typora写题解 真开心 参考 你会发现有很多都是参考的..zblzbl Codeforces Round #549 (Div. 1) 最近脑子不行啦 需要cf来缓解一下 A. The B ...
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- 【前行】◇第3站◇ Codeforces Round #512 Div2
[第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
随机推荐
- Excel开发学习笔记:读取xml文件及csv文件
遇到一个数据处理自动化的问题,于是打算开发一个基于excel的小工具.在业余时间一边自学一边实践,抽空把一些知识写下来以备今后参考,因为走的是盲人摸象的野路子,幼稚与错误请多包涵. ).Split( ...
- redux使用教程详细介绍
本文介绍redux的使用 安装 cnpm install redux --save cnpm install react-redux --save cnpm install redux-devtool ...
- CGContextRef详解
/* CoreGraphics - CGContext.h */ /** Graphics state functions. **/ //为了让开发者在进行坐标变换时无须计算多次坐标变换后的累加结果, ...
- Javascript面向对象(三):非构造函数的继承
这个系列的第一部分介绍了"封装",第二部分介绍了使用构造函数实现"继承". 今天是最后一个部分,介绍不使用构造函数实现"继承". 一.什么是 ...
- Linux split命令实例
曾经是否想要把一个大文件分割成多个小文件?比如一个5gb日志文件,我们需要把它分成多个小文件,这样我们才有可能使用普通的文本编辑器读取它.有时我们需要传输20gb的大文件到另一台服务器,这就需要我们把 ...
- Android开源地图项目 BigPlanetTracks 学习随笔
一. app主体部分 [tyt.android.bigplanettracks] 二. 地图部分 [tyt.android.bigplanettracks.maps] ...
- tortoisesvn 本项目的忽略项
https://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-dug-propertypage.html Adding and Editing Pr ...
- 在PyCharm 软件中设置你的项目 使用的Python版本
在PyCharm 软件中设置你的项目 使用的Python版本 python2 和 python3 有很大的不同,使用python2 编写的程序,如果使用python3 就运行不了:使用python3编 ...
- php学习笔记-超级全局变量
超级全局变量,超级在哪里呢?相对于global类型的变量,超级全局变量的作用域是没有限制的,函数外.函数内.随便一个PHP文件都可以引用超级全局变量.在PHP中有很多超级全局变量, 常用的有_SERV ...
- php学习笔记-关联数组
传统的数组定义方法如下: <?php $names[0]= 'chinese'; $names[1]= 'math'; $names[2]= 'english'; echo $names[2]; ...