5、 英文输入法
import java.util.*;
/*
使用该方法Collections.sort(Arrays.asList(strings))将字符串数组排好序,再遍历比对得出结果
*/
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] strings = sc.nextLine()
.replace("'"," ") //对“'”符号进行空格处理
.replace(",","")
.replace(".","")
.replace("?","")
.replace("!","") //对“, . ? !”符号进行删除处理
.split(" "); //按照空格进行分割
Collections.sort(Arrays.asList(strings)); //因为是字典需要进行排序
String key = sc.nextLine();
int keyLen = key.length();
int strLen = strings.length;
String res = "";
for(int i=0;i<strLen;i++){
String s = strings[i];
if(s.length()>=keyLen && s.substring(0,keyLen).equals(key)){ //匹配的字符串需要判断长度,并进行关键词长度的分割
if(res.length()!=0){
res+=" ";
}
res+=s;
}
}
if(res.length()==0){
res = key;
}
System.out.println(res);
}
}
6、 来自异国的客人
import java.util.Scanner;
// 使用Integer.toString(_,_)可将int转对应二进制数字,然后字符比较即可
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
int n = sc.nextInt();
int m = sc.nextInt();
if(n >= m){
System.out.println(0);
}else{
String str = Integer.toString( k, m);
int count = 0;
for(int i=0; i<str.length(); i++){
if(str.charAt(i) == String.valueOf(n).charAt(0)){
count ++;
}
}
System.out.println(count);
}
}
}
7、 整数最小和
import java.util.Scanner;
import java.util.Arrays;
// 穷举排序即可
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
//处理成int[]
String[] strArray1 = sc.nextLine().split(" ");
String[] strArray2 = sc.nextLine().split(" ");
int n = Integer.parseInt(strArray1[0]);
int m = Integer.parseInt(strArray2[0]);;
int[] array1 = new int[n];
int[] array2 = new int[m];
for (int i = 0; i < n; i++) {
array1[i] = Integer.parseInt(strArray1[i+1]);
}
for (int i = 0; i < m; i++) {
array2[i] = Integer.parseInt(strArray2[i+1]);
}
//初始化临时和数组
int[] res = new int[n*m];
for (int i = 0; i < n*m; i++) {
res[i] = Integer.MAX_VALUE;
}
//穷举结果
int l=0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
res[l++] = array1[i]+array2[j];
}
}
//排序
Arrays.sort(res);
//累加前k个结果
int sum = 0;
for (int i = 0; i < k; i++) {
sum+=res[i];
}
System.out.println(sum);
}
}
8、 游戏分组
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
import java.lang.Math;
// 五个嵌套循环,模拟随机从10个人里面抽取五个,遍历后得出最小差值
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
List<Integer> teamNumber = new ArrayList<>();
int sum = 0;
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
teamNumber.add(a);
sum += a;
}
int result = sum;
// System.out.println(sum);
//可以通过sum/2 剪纸
for(int one = 0; one < 10; one++){
for(int two = one+1; two < 10; two++){
int add12 = teamNumber.get(one) + teamNumber.get(two);
for(int three = two+1; three < 10; three++){
int add23 = teamNumber.get(three) + add12;
for(int four = three+1; four < 10; four++){
int add34 = teamNumber.get(four) + add23;
for(int five = four+1; five < 10; five++){
int add45 = teamNumber.get(five) + add34;
result = Math.min(result, Math.abs(sum-add45*2));
}
}
}
}
}
System.out.println(result);
}
}
9、 精准核酸检测
import java.util.*;
/**
使用递归从母体一个个往下找
*/
public class Main {
private static Set <Integer> set = new HashSet<>();
private static int res = 0;
private static int num = 0;
private static int[][] map;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
sc.nextLine();
String[] cs = sc.nextLine().split(",");
for (int i = 0; i < cs.length; i ++) {
set.add(Integer.parseInt(cs[i]));
}
int numReal = set.size();
map = new int [num] [num];
for (int i = 0; i < num; i ++) {
String[] csT = sc.nextLine().split(",");
for (int h = 0; h < csT.length; h ++) {
map[i][h] = Integer.parseInt(csT[h]);
}
}
for (String in : cs) {
search(Integer.parseInt(in));
}
System.out.println(res);
}
private static void search(int id) {
for (int i = 0; i < num; i ++) {
if (map[id][i] == 1 && !set.contains(i)) {
res ++;
set.add(i);
search(i);
}
}
}
}
10、 最多购买宝石数目
import java.util.*;
// 循环遍历即可
public class Main {
private static int[] nums;
private static int coinNum;
private static int max = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
nums = new int[num];
nums = new int[num];
for (int i = 0; i < num; i ++) {
sc.nextLine();
nums[i] = sc.nextInt();
}
coinNum = sc.nextInt();
for (int i = 0; i < num; i ++) {
max = Math.max(max, getCount(i));
}
System.out.println(max);
}
private static int getCount(int start) {
int coinTemp = coinNum;
int countTemp = 0;
for (int i = start; i < nums.length; i ++) {
if (coinTemp - nums[i] < 0) {
break;
} else {
coinTemp -= nums[i];
countTemp ++;
}
}
return countTemp;
}
}
加急更新中。。。