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 ...
随机推荐
- c++ 端口扫描程序
第一.原理 端口扫描的原理很简单,就是建立socket通信,切换不通端口,通过connect函数,如果成功则代表端口开发者,否则端口关闭. 所有需要多socket程序熟悉,本内容是在window环境下 ...
- 监控和安全运维 1.2 cacti安装
---恢复内容开始--- 一.安装cacti服务器 1.首先安装epm扩展源 rpm -ivh http://www.lishiming.net/data/attachment/forum/epel- ...
- CUDA编程接口:异步并发执行的概念和API
1.主机和设备间异步执行 为了易于使用主机和设备间的异步执行,一些函数是异步的:在设备完全完成任务前,控制已经返回给主机线程了.它们是: 内核发射; 设备间数据拷贝函数; 主机和设备内拷贝小于64KB ...
- sys模块 进度条百分比
用于提供对Python解释器相关的操作: sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) ...
- 关于RAW 和 ASSEST文件夹的差异
以下内容转自:http://www.cnblogs.com/leizhenzi/archive/2011/10/18/2216428.html *res/raw和assets的相同点: 1.两者目录下 ...
- 动态参数 名称空间 作用域 作用域链 加载顺序 函数的嵌套 global nonlocal 等的用法总结
03,动态参数 *args,**kwargs # 用户传入到函数中的实参数量不定时,或者是为了以后拓展,# 此时要用到动态参数*args,**kwargs(万能参数.)# *args接收的是所有的位置 ...
- mongodb 降序
降序排序 db.mycol.find({},{,_id:}).sort({})// 1用于升序排列,而-1用于降序. 示例代码: var user = await Kitten.find({ type ...
- iOS 聊天界面
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- JavaPersistenceWithMyBatis3笔记-第3章SQL Mappers Using XMLs-001
一. 1.Mapper 2.Service 3.Domain package com.mybatis3.domain; import java.io.Serializable; import java ...
- Linux cpulimit命令
一.简介 http://www.3lian.com/edu/2015/06-12/221261.html 二.安装 http://toutiao.com/a6184908783490073090/ 三 ...