# 牛客网笔试题

## 三角形个数

ans

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {//注意while处理多个case
int N = in.nextInt();
int result = countTriangles(N);
System.out.println(result);
}
}

private static int countTriangles(int N){
if(N == 0 || N == 1) return N;

int before = 1;
int cur =  0;
while(N>1){
int one = 2*N-1;  //包含最后一层的边长为1的三角形个数
int upright = N*(N-1)/2; //包含最后一层的其他边长的正立三角形个数
int upsideDown = 0;
if((N&1)==1) upsideDown = (N-1)*(N-3)/4; //N为奇数时，包含最后一层的其他边长的倒立三角形个数
else upsideDown = (N-2)*(N-2)/4;  //N为偶数时，包含最后一层的其他边长的倒立三角形个数
cur = one + upright + upsideDown + before;
before = cur;
N--;
}

return cur;
}

}

## 钞票组合

2
50
1 15 20
3 30 30

2（一种是1+1+1=3，一种是1+3=4）
ans

import java.util.*;
import java.util.Map.Entry;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int N = in.nextInt(); //输入钞票种类个数
int M = in.nextInt(); //输入要组合得到的重量
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
for(int i = 0; i < N; i++) {
int value = in.nextInt();
int minWeight = in.nextInt();
int maxWeight = in.nextInt();
List<Integer> list  = new ArrayList<Integer>();
map.put(value, list);
}

Set<Integer> moneySum = new HashSet<Integer>();
backtracking(map, moneySum, 0, 0,M);
System.out.println(moneySum);
System.out.println(moneySum.size());
}
}

private static void backtracking(Map<Integer, List<Integer>> map, Set<Integer> moneySum, int curMoneySum,int curWeightSum, int target){
if(curWeightSum>target) return;
if(curWeightSum==target){
return;
}

Iterator<Map.Entry<Integer, List<Integer>>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Integer, List<Integer>> entry =  iterator.next();
int newMoneySum = curMoneySum + entry.getKey();
int newWeightSum1 = curWeightSum + entry.getValue().get(0);
int newWeightSum2 = curWeightSum + entry.getValue().get(1);
backtracking(map, moneySum, newMoneySum, newWeightSum1, target);
backtracking(map, moneySum, newMoneySum, newWeightSum2, target);
}
}

}

## 生成回文

4（数列中数字个数）
[3 1 1 1]

3
try

import java.util.*;
//由于牛客网通常都会对输入范围作出限制，所有省去了一些防御型代码==随意说一下==
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int N = in.nextInt();
int[] numbers = new int[N];
for(int i=0;i<N;i++){
numbers[i] = in.nextInt();
}

int cnt = countTimes(numbers);
System.out.println(cnt);
}
}

private static int countTimes(int[] numbers){
int cnt = 0;
int fromIndex = 0;
int toIndex = numbers.length-1;
while(!isPalindrome(numbers) && fromIndex<numbers.length/2){
int index = findIndexOfMinNumber(numbers, fromIndex, toIndex);
if(index<numbers.length/2){//位于左半部分，与后面那位相加，用二者的和替换这两个数
numbers[index] = numbers[index]+numbers[index+1];
numbers[index+1] = numbers[index];
fromIndex = index;
toIndex = (numbers.length-1)-fromIndex;
}
else{//位于右半部分，与前面那位相加，用二者的和替换这两个数
numbers[index] = numbers[index]+numbers[index-1];
numbers[index-1] = numbers[index];
toIndex = index;
fromIndex = (numbers.length-1)-toIndex;
}
cnt++;
}
return cnt;
}

private static boolean isPalindrome(int[] numbers){
for(int i=0,j=numbers.length-1; i<j;i++,j--){
if(numbers[i]!=numbers[j]) return false;
}
return true;
}

private static int findIndexOfMinNumber(int[] numbers, int fromIndex, int toIndex){
while(fromIndex<toIndex){
if(numbers[fromIndex] == numbers[toIndex] ) {
fromIndex++;
toIndex--;
}
else break;
}
return (numbers[fromIndex]<numbers[toIndex])?fromIndex:toIndex;
}
}

## 最短完美子串

（超时了….目测应该用动态规划…）

## 字符组合

str1 str2……..strN（N个字符串，每个字符串内的字符介于0~5之间）

（有点乱。。原题更biantai，长长的故事）

01 23

01 10

3 015 5

ans

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int N = in.nextInt(); //输入派出的战士个数
List<List<Integer>> defendNumbers = new ArrayList<List<Integer>>();
for(int i = 0; i < N; i++) {
String str = in.next();
ArrayList<Integer> strList = new ArrayList<Integer>(); //每个战士能够对抗的敌人编号(0~6)
for(char c: str.toCharArray()) {
}
}
int cnt = generateMethod(defendNumbers, N-1).size();
System.out.println(cnt);
}
}

public static List<Set<Integer>> generateMethod(List<List<Integer>> defendNumbers, int i) {
if(i == 0) {
List<Set<Integer>> res = new ArrayList<Set<Integer>>();
for(int enermy: defendNumbers.get(0)) {
Set<Integer> enermys = new HashSet<Integer>();
}
return res;
} else {
List<Set<Integer>> previousEnermySet = generateMethod(defendNumbers, i-1);
List<Set<Integer>> res = new ArrayList<Set<Integer>>();
for(Set<Integer> previousEnermys: previousEnermySet) {
for(int enermy: defendNumbers.get(i)) {
if(!previousEnermys.contains(enermy)) {
Set<Integer> newEnermys = new HashSet<Integer>();
}
}
}
return res;
}
}
}

08-17 1万+

06-04 2728

08-26 1万+

03-23 3360

#### 牛客网面试试题华为

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。