2017阿里编程测试题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/boom_man/article/details/77519675

题目:
在某射击场有N个靶,每个靶上都有一个分数,存在score数组中。击中第i个靶的得分为score[left] * score[i] * score[right],同时原left和right两个靶变为相邻的靶。其中得分为0的靶是不能射击的,当left不存在或者不能射击时,得分为 score[i] * score[right],同理right也遵循此规则; 当left和right都不存在或者不能射击时,得分为score[i]。请计算出击中所有能射击的靶,最多能得多少分?
输入范例:
4
2
3
0
3
输出范例:
12

import java.util.*;

public class Main {

/** 请完成下面这个函数,实现题目要求的功能 **/
    /** 当然,你也可以不按照这个模板来作答,完全按照自己的想法来 ^-^  **/
    static int maxScore(int[] score) {
        int scoreMax=0;

        return scoreMax;
    }

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int res;

        int _score_size = 0;
        //输入靶的个数
        _score_size = Integer.parseInt(in.nextLine().trim());
        int[] _score = new int[_score_size];
        int _score_item;
        for(int _score_i = 0; _score_i < _score_size; _score_i++) {
            //依次输入每个靶的得分,
            _score_item = Integer.parseInt(in.nextLine().trim());
            _score[_score_i] = _score_item;
        }

        res = maxScore(_score);
        System.out.println(String.valueOf(res));

    }
}

题目类似 LeetCode-312

下面把问题如何分解到LeetCode这道题上

思路将靶得分为0将靶分开成几部分,然后带入LeetCode-312解法中即可

import java.util.ArrayList;

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int res;

        int _score_size = 0;
        //输入靶的个数
        _score_size = Integer.parseInt(in.nextLine().trim());
        int[] _score = new int[_score_size];
        int _score_item;
        for(int _score_i = 0; _score_i < _score_size; _score_i++) {
            //依次输入每个靶的得分,
            _score_item = Integer.parseInt(in.nextLine().trim());
            _score[_score_i] = _score_item;
        }

        res = getMax(_score);
        System.out.println(String.valueOf(res));
    }


    private static int getMax(int [] num){
        ArrayList<Integer> arrayList=new ArrayList<>();
        for (int i = 0; i < num.length; i++) {
            if (i==num.length-1&num[i]!=0){
                arrayList.add(i);
            }else if (num[i]==0){
                arrayList.add(i);
            }else if (i==0){
                arrayList.add(0);
            }
        }
        int sum=0;
        for (int i=0;i<arrayList.size()-1;i++){
            System.out.println("开始位置:"+arrayList.get(i)+"结束位置:"+arrayList.get(i+1));
            sum+=getMaxScore(num,arrayList.get(i),arrayList.get(i+1));
        }
        return sum;
    }


    private static int getMaxScore(int[] num,int start,int end) {

        int[] newNum=new int[end-start+3];
        int n=1;

        for (int i = start; i <=end; i++) {
            newNum[n++]=num[i];
        }

        newNum[0]=newNum[n++]=1;

        int[][] dp=new int[n][n];
        //用于记录击中顺序
        int[][] burstorders = new int[n][n];
        for (int disappear  = 2; disappear <n ; disappear++) {
            for (int left = 0; left < n - disappear; left++) {
                int right=left+disappear;
                for (int i = left+1; i < right; i++) {
                    //如何知道扎破顺序
                    int score=dp[left][i]+newNum[left]*newNum[i]*newNum[right]+dp[i][right];
                    if(score>dp[left][right]){
                        dp[left][right]=score;
                    }

                }
            }
        }
        return dp[0][n-1];
    }
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页