zoukankan      html  css  js  c++  java
  • java实现 洛谷 P1018 乘积最大

    在这里插入图片描述

    import java.math.BigInteger;
    import java.util.Scanner;
     
    public class Main {
    	private static Scanner cin;
    	private static char[] values;
    	private static BigInteger max = new BigInteger("1");
    	private static int n;
    	private static int k;
    	
    	public static void main(String args[]) throws Exception {
    		cin = new Scanner(System.in);
    		n = cin.nextInt();
    		k = cin.nextInt();
    		String nvalue = cin.next();
    		values = nvalue.toCharArray();
    		calc(0,n-k,max,1);
    		System.out.println(max);
    	}
    	
    	/**
    	 * 基于values数组,从startposition位置开始,有length个数字可以用来分割拼接成数字,当前处理到k+1个数字的第count个,tmpValue是之前count-1个数字的乘积
    	 * @param startPosition
    	 * @param length
    	 * @param tmpValue
    	 * @param count
    	 */
    	public static void calc(int startPosition, int length, BigInteger tmpValue, int count) {		
    		String tmp = null;
    		//如果是最后一个数,使用剩余的数字组成一个数字
    		if(count == k+1) {
    			tmp =String.valueOf(values,startPosition,values.length-startPosition);
    			BigInteger value = new BigInteger(tmp);
    			BigInteger tValue = tmpValue.multiply(value);
    			if(tValue.compareTo(max)>0) {
    				max = tValue;
    			}
    		}else {
    			for(int j=1;j<=length;j++) {
    				tmp = String.valueOf(values,startPosition,j);
    				BigInteger value = new BigInteger(tmp);
    				BigInteger tValue = tmpValue.multiply(value);
    				calc(startPosition+j,n-startPosition-j-(k-count),tValue,count+1);
    			}
    		}
    	}
    }
    
  • 相关阅读:
    CF710F String Set Queries AC自动机 二进制分组
    类欧几里得学习笔记
    P2053 [SCOI2007]修车 网络流
    螺旋方阵
    灯的排列问题
    编码问题
    论文阅读博客模板
    论文阅读框架模板
    动作识别论文20191104_Probabilistic selection of frames for early action recognition in videos
    剑指offer 57. 数字序列中某一位的数字
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13076278.html
Copyright © 2011-2022 走看看