目录
1、三个数的最大乘积
题目:力扣
题解:
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.xml.transform.sax.SAXTransformerFactory;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author yx
* @date 2022-07-02 10:05
*/
public class lc_628三个数的最大乘积 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i=scanner.nextInt();
int[] nums=new int[i];
for (int j = 0; j < i; j++) {
nums[j]=scanner.nextInt();
}
}
static int maximumProduct(int[] nums,int n) {
Arrays.sort(nums);//sort使用的是升序排序
System.out.print("排序后的数组:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i]+" ");
}
return(Math.max(nums[0]*nums[1]*nums[n-1],nums[n-1]*nums[n-2]*nums[n-3]));
}
}
2、有多少小于当前数字的数字
题解:
import java.lang.reflect.Array;
import java.util.*;
/**
* @author yx
* @date 2022-07-02 10:51
*/
public class lc_1365有多少小于当前数字的数字 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i=scanner.nextInt();
int[] nums=new int[i];
for (int j = 0; j < i; j++) {
nums[j]=scanner.nextInt();
}
smallerNumbersThanCurrent(nums);
}
static public void smallerNumbersThanCurrent(int[] nums) {
int []ans=Arrays.copyOf(nums,nums.length);
Arrays.sort(ans);
// 用一个hashmap存数组内容和下标
Map<Integer,Integer> map=new HashMap<>();
for (int i = 0; i < ans.length; i++) {
if (!map.containsKey(ans[i])) {//解决数字出现重复的情况
map.put(ans[i],i);
}
}
for (int i = 0; i < nums.length; i++) {
nums[i]=map.get(nums[i]);
// System.out.print(nums[i]);
}
}
}
3、使数组唯一的最小增量
题解:
package 暑期特训__社区;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author yx
* @date 2022-07-02 12:14
*/
public class lc_945使数组唯一的最小增量 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n=scanner.nextInt();
int[] nums=new int[n];
for (int i = 0; i < n; i++) {
nums[i]=scanner.nextInt();
}
minIncrementForUnique(nums);
}
static public void minIncrementForUnique(int[] nums) {
int ans=0;//记录操作次数
Arrays.sort(nums);
for (int i = 1; i < nums.length; i++) {
if(nums[i]<=nums[i-1]){
ans+=nums[i-1]-nums[i]+1;
nums[i]=nums[i-1]+1;
}
}
System.out.println(ans);
}
}
4、B. Rising Sand
题解:
package 暑期特训__社区;
import java.util.Scanner;
/**
* @author yx
* @date 2022-07-02 16:07
*/
public class cf_B_RisingSand {
public static void main (String[]args){
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int j = 0; j < t; j++) {
int n = scanner.nextInt();
int k = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
if (k == 1) {
System.out.println((n - 1) / 2);
continue;
}
int ans = 0;
for (int i = 1; i < n - 1; i++) {
if (a[i] > a[i - 1] + a[i + 1]) ans++;
}
System.out.println(ans);
}
}
}