HRBUST1212 乘积最大 2017-03-06 15:47 59人阅读 评论(0) 收藏
乘积最大
设有一个长度为N的数字串,要求选手使用K个乘号将它分成K+1个部分,找出一种分法,使得这K+1个部分的乘积能够为最大。
同时,为了帮助选手能够正确理解题意,主持人还举了如下的一个例子:
有一个数字串:312, 当N=3,K=1时会有以下两种分法:
1) 3*12=36
2) 31*2=62
这时,符合题目要求的结果是:31*2=62
现在,请你帮助你的好朋友XZ设计一个程序,求得正确的答案。
对于每组测试数据有两行:
第一行共有2个自然数N,K(6≤N≤40,1≤K≤6)
第二行是一个长度为N的数字串。
1231
——————————————————————————————————
题目的意思是把长度为n的数字中插入k和*,使积最大
dfs找暴力枚举*位置,数字较大,java大数运算
import java.math.BigInteger;
import java.util.Scanner; public class Main { static int n;
static int m;
static BigInteger r,r1,ans;
static BigInteger r2=new BigInteger("10");
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{
n=cin.nextInt();
m=cin.nextInt();
r=cin.nextBigInteger();
ans=new BigInteger("-999");
dfs(0,0,new BigInteger("0"),new BigInteger("0"));
System.out.println(ans);
} }
public static void dfs(int cur,int k,BigInteger num,BigInteger temp)
{
if(cur==n)
{
if(temp!=new BigInteger("0"))
num=num.multiply(temp);
if(k==2)
{
ans=ans.max(num);
}
return;
}
char c=r.toString().toCharArray()[cur];
int t=c-'0';
long a=Integer.valueOf(t);
BigInteger r3=BigInteger.valueOf(a); dfs(cur+1,k,num,temp.multiply(r2).add(r3));
if(k!=0)
dfs(cur+1,k+1,num.multiply(temp),r3);
else dfs(cur+1,k+1,temp,r3); } }
HRBUST1212 乘积最大 2017-03-06 15:47 59人阅读 评论(0) 收藏的更多相关文章
- PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...
- .net 实现Office文件预览 Word PPT Excel 2015-01-23 08:47 63人阅读 评论(0) 收藏
先打个广告: .Net交流群:252713569 本人QQ :524808775 欢迎技术探讨, 近期公司要求上传的PPT和Word都需要可以在线预览.. 小弟我是从来没有接触过这一块的东西 感觉很棘 ...
- Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏
Basic Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 905 Accepted: 228 Description The p ...
- Gold Coins 分类: POJ 2015-06-10 15:04 16人阅读 评论(0) 收藏
Gold Coins Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21767 Accepted: 13641 Desc ...
- Speed Limit 分类: POJ 2015-06-09 17:47 9人阅读 评论(0) 收藏
Speed Limit Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17967 Accepted: 12596 Des ...
- Transact-SQL 常用函数 分类: SQL Server 2015-02-03 09:47 284人阅读 评论(0) 收藏
(1)DECLARE 两种用法: 1>: DECLARE @usid VARCHAR(50),@usna NVARCHAR(100),@grna NVARCHAR(100); 2>: DE ...
- 苹果应用商店AppStore审核中文指南 分类: ios相关 app相关 2015-07-27 15:33 84人阅读 评论(0) 收藏
目录 1. 条款与条件 2. 功能 3. 元数据.评级与排名 4. 位置 5. 推送通知 6. 游戏中心 7. 广告 8. 商标与商业外观 9. 媒体内容 10. 用户界面 11. 购买与货币 12. ...
- Codeforces816A Karen and Morning 2017-06-27 15:11 43人阅读 评论(0) 收藏
A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standar ...
- Codeforces812B Sagheer, the Hausmeister 2017-06-02 20:47 85人阅读 评论(0) 收藏
B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- 进程基本-进程创建,僵尸进程,exec系列函数
Linux系统中,进程的执行模式划分为用户模式和内核模式,当进程运行于用户空间时属于用户模式,如果在用户程序运行过程中出现系统调用或者发生中断事件,就要运行操作系统(即核心)程序,进程的运行模式就变为 ...
- 峰Spring4学习(8)spring对事务的支持
一.事务简介: 二.编程式事务管理: 例子 1.需求:模拟转账,张三向李四转账50元: 数据库中存在t_count表: 代码实现: BankDao.java: package com.cy.dao; ...
- Bootstrap-Other:v2 教程
ylbtech-Bootstrap-Other:v2 教程 1.返回顶部 1. Bootstrap v2 教程 Bootstrap,来自 Twitter,是基于 HTML.CSS.JAVASCRIPT ...
- [Spring] Resource 资源
import ch.qos.logback.core.net.SyslogOutputStream; import org.springframework.core.io.ClassPathResou ...
- hibench学习
hibench包含几个hadoop的负载 micro benchmarksSort:使用hadoop randomtextwriter生成数据,并对数据进行排序. Wordcount:统计输入数据中每 ...
- 关于SSE的一些资料
之前查到的,觉得还不错,一直没时间看,放这里先存着 https://software.intel.com/en-us/articles/using-intel-streaming-simd-exten ...
- jsp 学习 第1步 - 引入 jstl
通过 eclipse 新建 动态web项目 默认是没有引入 jstl, 则无法JSP页面引入相关标记. <%@ taglib prefix="c" uri="ht ...
- np的归纳总结
1. np.sqrt(input) # 求数的开方 import numpy as np print(np.sqrt(2)) 2. np.square(3) # 求数的平方 import n ...
- Tutorial: Getting Started with Spring Security
This tutorial will cover a basic scenario where it integrates Spring Security, using database-backe ...
- maven 中的依赖