1.小美的礼盒数量
小美做饼干,有AB两种,做成礼盒卖,每个礼盒三个饼干,且至少包含一个A 和 一个B。现在给你A和B的数量,问最多可以做多少礼盒?
思路:
分析了一下,礼盒的数量为(A,B,(A+B)/3)三者的最小值。
import java.util.Scanner;
public class Q1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
for (int i = 0; i < N; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println(getCount(x,y));
}
}
public static int getCount(int A,int B){
//假设能做x盒,那么 x<=A x<=B 且 3x<=A+B
int x = (A+B)/3;
x = Math.min(x,A);
x = Math.min(x,B);
return x;
}
}
2.小美做实验
小美做实验,有一个纸带上有若干个数字,在纸带上选择一个位置k,k是分割点(比如k选3,那么左边是123,右边是456),k左边大于等于0的点为异常点,k右边小于等于0的点为异常点。
现在给一个纸带,不给k,问你最乐观情况下异常点最少个数为多少(选哪个k异常点最少吧)
思路:
用前缀数组,记录每个点左边的异常点,再从右边计算异常点回来,每次左右异常点个数相加,取最小一个。
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int arr[] = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = sc.nextInt();
}
System.out.println(minException(arr));
}
public static int minException(int arr[]){
int n = arr.length;
int ans = Integer.MAX_VALUE;
int leftException[] = new int[n];
leftException[0] = 0;
for(int i = 1; i < n; i++){
leftException[i] = leftException[i-1];
if(arr[i-1]>=0){
leftException[i]++;
}
}
// printArr("leftException",leftException);
int rightException = 0;
for(int i = n-1; i>=0; i--){
if(arr[i]<=0) rightException++;
ans = Math.min(ans,leftException[i]+rightException);
}
return ans;
}
public static void printArr(String str ,int arr[]){
System.out.println(str);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
}
System.out.println();
}
}
3.小美魔法石
小美有n个魔法石,每个魔法石有正反两个数字,一开始全部正面朝上,魔法石要激活需要有一半以上的魔法石数字相同,问你把魔法石激活的最小翻牌次数(把魔法石从正面变反面)
思路:
计每个牌出现的数量,因为能出现一半以上的最多四种吧,然后去遍历能出现一半以上的数量,然后去扣掉正面牌上这个数的数量,就是需要翻牌的数量。然后找最小
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int up[] = new int[n];
int down[] = new int[n];
for (int i = 0; i < n; i++) {
up[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
down[i] = sc.nextInt();
}
System.out.println(minOps(n,up,down));
}
public static int minOps(int n, int[] up, int[] down){
HashMap<Integer,Integer> allMap = new HashMap<>();
HashMap<Integer,Integer> upMap = new HashMap<>();
//先统计个数
for (int i = 0; i < n; i++) {
//两面相同统计一次
allMap.put(up[i],allMap.getOrDefault(up[i],0)+1);
if(up[i]!=down[i]){
allMap.put(down[i],allMap.getOrDefault(down[i],0)+1);
}
upMap.put(up[i],upMap.getOrDefault(up[i],0)+1);
}
int target = n%2==0?n/2:n/2+1;
int ans = n+1;
for (Integer key : allMap.keySet()) {
if(allMap.get(key)>=target){
ans = Math.min(ans,upMap.getOrDefault(key,0)>=target?0:target-upMap.getOrDefault(key,0));
}
}
return ans==n+1?-1:ans;
}
}
4.小美训练集
按顺序给你一堆训练集(只有类别编号),就是给了一个数组,然后每个类别中,前(类别数据个数)/2向上取整为训练集,后面的是测试集,让我们按顺序拆分
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Q4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int cnt[] = new int[k+1];
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
cnt[arr[i]]++;
}
int order[] = new int[k+1];//表示k当前访问的个数
List<Integer> train = new ArrayList<>();
List<Integer> test = new ArrayList<>();
for (int i = 0; i < n; i++) {
order[arr[i]]++;
if(order[arr[i]] <= (cnt[arr[i]]%2==0?cnt[arr[i]]/2:cnt[arr[i]]/2+1)){
train.add(i+1);
}
else{
test.add(i+1);
}
}
for (int i = 0; i < train.size()-1; i++) {
System.out.print(train.get(i)+" ");
}
if(train.size()>0)
System.out.println(train.get(train.size()-1));
else{
System.out.println();
}
for (int i = 0; i < test.size()-1; i++) {
System.out.print(test.get(i)+" ");
}
if(test.size()>0) System.out.println(test.get(test.size()-1));
else System.out.println();
}
}
5.小美 字符串
初始字符串为MetTuan,每次对字符串做 str = str + str.reverse() + "wow"的操作,无限循环。后面给你一个k,问你位置k的字符为什么。
思路
找规律,无限循环后 是 str+str.reverse()+"wowwow"的无限循环
import java.util.Scanner;
public class Q5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
String str = "MeiTuan";
str = str + (new StringBuilder(str).reverse().toString())+"wowwow";
for (int i = 0; i < T; i++) {
long pos = sc.nextLong()-1;
pos = pos % str.length();
System.out.println(str.charAt((int)pos));
}
}
public static String function(String str){
return str + (new StringBuilder(str).reverse().toString()) + "wow";
}
/*
s
s fs wow
(s fs wow) (wow s fs) wow
[(s fs wow) (wow s fs) wow][wow (s fs) wow wow (s fs)] wow
{[(s fs wow) (wow s fs) wow][wow (s fs) wow wow (s fs)] wow} {wow s fs wow wow s fs wow wow s fs wow wow sfs w
*/
}