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 ...
随机推荐
- 卸载驱动时,没有/lib/modules目录
卸载驱动时,没有/lib/modules目录,怎么办? 答:没有这个目录,就创建这个目录! #rmmod spi_oled_drv #rmmod: chdir (/lib/modules): no ...
- IOS 屏幕尺寸
型号 屏幕尺寸(英寸) 分辨率(pt) 像素分辨率(px)iPhone 3G 3.5 320*480 480x320iPhone 3GS 3.5 320*480 480x320iPhone4 3. ...
- mysql权限表
http://blog.csdn.net/zhangliangzi/article/details/51882710
- Stun方式的P2P实现原理(转)
转帖地址:http://www.cppblog.com/peakflys/archive/2013/01/25/197562.html 二.STUN方式的P2P实现 STUN是RFC3489规定的 ...
- DAY9-python并发之多线程
一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍 官网链接:https://docs.python ...
- ubuntu16搭建docker私库
测试环境如下: 一.docker的安装 安装方法请查看这里的 安装教程 二.设置普通用户 1. centos的设置方法 $ sudo gpasswd -a docker ${USER} 2. ubun ...
- 关于service和线程的区别
主要有两方面,访问控制和功能区别 首先,service是运行在主线程上的,并不是一个新的线程 其次,service在运行的时候可以被多个activity访问和控制,而线程是不可以的 最后,servic ...
- Android禁止程序自动旋转的配置
在想要禁止的Activity中加入 android:screenOrientation="portrait" 属性,其中,portrait是竖屏,landscape是横屏
- 文科妹学 GitHub 简易教程(转)
文科妹学 GitHub 简易教程 #什么是 Github ?必须要放这张图了!!! Git 是由 Linux 之父 Linus Tovalds 为了更好地管理linux内核开发而创立的分布式版 ...
- LINUX关闭防火墙、开放特定端口等常用操作
1. 重启后永久性生效: 开启:chkconfig iptables on 关闭:chkconfig iptables off 2. 即时生效,重启后失效: 开启:service iptables s ...