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 滿足要求)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-659206.html
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)!