国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

華為OD真題--分月餅--帶答案

這篇具有很好參考價(jià)值的文章主要介紹了華為OD真題--分月餅--帶答案。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

1. 華為OD機(jī)考題 + 答案

2023華為OD統(tǒng)一考試(A+B卷)題庫(kù)清單-帶答案(持續(xù)更新)

2023年華為OD真題機(jī)考題庫(kù)大全-帶答案(持續(xù)更新)

2. 面試題

一手真實(shí)java面試題:2023年各大公司java面試真題匯總--持續(xù)更新

3. 技術(shù)知識(shí)

java后端技術(shù)匯總 + 中間件 + 架構(gòu)思想

題目描述:

公司分月餅,m個(gè)員工,買了n個(gè)月餅,m <= n,每個(gè)員工至少分一個(gè)月餅,但是也可以分到多個(gè),單人分到最多月餅的個(gè)數(shù)是Max1,單人分到第二多月餅個(gè)數(shù)是Max2。

但需要滿足Max1-Max2 <= 3,單人分到第n-1多月餅個(gè)數(shù)是Max(n-1),單人分到第n多月餅個(gè)數(shù)是Max(n), 想要滿足Max(n-1) - Max(n) <= 3,問有多少種分月餅的方法?

輸入描述:

每一行輸入m,n,表示m個(gè)員工,n個(gè)月餅,m <=n

輸出描述:

輸出有多少種分法

示例1:

輸入

2 4

輸出

2

說(shuō)明

4=1+3

4=2+2

注意:1+3和3+1要算成同一種分法

示例2:

輸入

3 5

輸出

2

說(shuō)明

5=1+1+3

5=1+2+3

示例3:

輸入

3 12

輸出

6

說(shuō)明

滿足要求的6種分法:

1、12 = 1 + 1 + 10 (Max1=10, Max2=1,不滿足Max1-Max2 <= 3的約束)

2、12 = 1 + 2 + 9 (Max1=9,Max2=2,不滿足Max1-Max2 <= 3的約束)

3、12 = 1 + 3 + 8 (Max1=8,Max2=3,不滿足Max1-Max2 <= 3的約束)

4、12 = 1 + 4 + 7 (Max1=7,Max2=4,Max3=1, 滿足要求)

5、12 = 1 + 5 + 6 (Max1=6,Max2=5,Max3=1, 不滿足要求)

6、12 = 2 + 2 + 8 (Max1=8,Max2=2,不滿足要求)

7、12 = 2 + 3 + 7 (Max1=7,Max2=3,不滿足要求)

8、12 = 2 + 4 + 6 (Max1=6,Max2=4,Max3=2, 滿足要求)

9、12 = 2 + 5 + 5 (Max1=5,Max2=2 滿足要求)

10、12 = 3 + 3 + 6 (Max1=6,Max2=3 滿足要求)

11、12 = 3 + 4 + 5 (Max1=5,Max2=4,Max3=3 滿足要求)

12 = 4 + 4 + 4 (Max1=4,滿足要求)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-659206.html

public class DivideMooncake {
//非最優(yōu)解,需要考慮減枝,減少遍歷次數(shù)。
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int [] num = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        //分配人數(shù)
        int peoples = num[0];
        //待分配數(shù)量
        int dnum = num[1] - num[0];
        //重新分配后每個(gè)人月餅數(shù)量
        int [] nums = new int[peoples];
        // 用一個(gè)List來(lái)存儲(chǔ)所有分配方案
        List<List<Integer>> result = new ArrayList<>();
        divide(dnum,peoples,nums,result);
        List<List<Integer>>filteredResult = removeDuplicate(result);

        //System.out.println(filteredResult);
        System.out.println(filteredResult.size());
    }

    public static void divide(int num,int peolpe, int [] nums,List<List<Integer>> result){
        if (peolpe == 1){
            nums[0] = num;
            List<Integer> allocation = new ArrayList<>();
            for (int i : nums){
                allocation.add(i);
            }
            result.add(allocation);
            return;
        }

        for (int i = 0; i <= num ; i++){
            //要分配的月餅
            nums[peolpe - 1] = i;
            //遞歸調(diào)用,將要分配的月餅分配給其它人
            divide(num - i,peolpe -1,nums,result);
        }

    }

    /**
     * 判斷是否滿足條件  Max(n) - Max(n-1) >= 3
     * @param nums
     * @return
     */
    public static Boolean satisfy(List<Integer> nums){
        int i = nums.size() -1;
        while (i >= 1){
            if (nums.get(i) - nums.get(i - 1) > 3){
                return false;
            }
            i--;
        }
        return true;
    }


    /**
     * 分?jǐn)?shù)一致的去重
     * @param result
     * @return
     */
    public static List<List<Integer>> removeDuplicate(List<List<Integer>> result) {
        List<List<Integer>> filteredResult = new ArrayList<>();

        for (List<Integer> allocation : result) {
            boolean duplicate = false;
            allocation.sort(Integer::compareTo); // 對(duì)分配方案進(jìn)行排序

            for (List<Integer> existingAllocation : filteredResult) {
                existingAllocation.sort(Integer::compareTo); // 對(duì)已有的分配方案進(jìn)行排序

                if (Arrays.equals(existingAllocation.toArray(), allocation.toArray())) {
                    duplicate = true; // 分配方案重復(fù)
                    break;
                }
            }

            if (!duplicate) {
                if (satisfy(allocation)){
                    filteredResult.add(allocation);
                }
            }
        }

        return filteredResult;
    }

}

到了這里,關(guān)于華為OD真題--分月餅--帶答案的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包