教育改变生活

标题: Java编程-剪绳子 [打印本页]

作者: 一秉    时间: 2020-9-30 11:36
标题: Java编程-剪绳子
题目描述:
给你一根长度为n的绳子,请把绳子剪成整数长的m段(m、n都是整数,n>1并且m>1,m<=n),每段绳子的长度记为k[1],...,k[m]。请问k[1]x...xk[m]可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。
输入描述:输入一个数n。(2 <= n <= 60)
输出描述:输出答案。
示例1
输入:8
输出:18
解题代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
  
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        System.out.println(cutRope(n));
    }
    private static int cutRope(int target){
        int a = 0;
        int c = 0;
        int maxValue = 2;
         
        if (target == 2){
            return 1;
        }
        if (target == 3) {
           return 2;
       }
        if (target % 3 == 0){
            maxValue = (int)Math.pow(3,target / 3);
        } else{
            a = target - 2;
            c = a % 3;
              
            maxValue = maxValue * (int)Math.pow(3,a / 3);
            if(0 != c){
                maxValue = maxValue *c;
            }
        }
         
        return maxValue;
    }
}





欢迎光临 教育改变生活 (http://bbs.goldoar.com/) Powered by Discuz! X3.2