华为题,放芯片
假设有n块芯片,负责AB两种业务。其中,每块芯片只能是运行4个A业务,或者1个B业务。
其中,不可以既执行A业务,由执行B业务,即二者是独立的
输入为三行,第一行芯片数量,第二行业务数量
第三行A B A B A这种描述各种业务的
大佬的思路,拜读
// 输入为芯片数量以及业务集合public void compute1(int nums_core, String server){// 分别记录装满的芯片id,可以引申为当前B装配的芯片ID// curr_id记录当前没有装满的id,引申为当前A放置的芯片// curr_opt指A放置的芯片中,目前放置在第几位int full_id = 0, curr_id = 0, curr_opt = 0;char opt = ' ';for (int i = 0; i< server.length(); i++) {// 获取当前元素opt = server.charAt(i);if (opt == 'A') {// 如果当前芯片没有存储的话,初始化的作用if (curr_opt == 0){curr_id = full_id +1;}// 如果是正常情况,在芯片中的编号++curr_opt++;// 否则需要另开一块芯片,只能放置在放满的后面if (curr_opt == 5) {curr_id = full_id + 1;curr_opt = 1;}full_id = Math.max(full_id, curr_id);}else {full_id++;}}if (Math.max(full_id, curr_id) > nums_core) {System.out.println(0);System.out.println(0);}else if (opt == 'B'){System.out.println(full_id);System.out.println(1);}else{System.out.println(curr_id);System.out.println(curr_opt);}}
进制转换
阿里题,对于输入的一个数,不知道具体是什么进制的,那么就按照2~16进制将其对10进制进行转换,然后输出。输出的数据很大,需要对10e9+7取模
public static void main(String[] args) {Scanner scanner = new Scanner(System.in);Main main = new Main();String next = scanner.next();char[] array = next.toCharArray();ArrayList<Integer> input = new ArrayList<>(array.length);for (char c : array) {if (c > '0' && c < '9')input.add(c - '0');elseinput.add(c - 'A' + 10);}Integer max = input.stream().max((o1, o2) -> o1 - o2).get();main.change(input, max);System.out.println((int) (1.004336277661869e+59 % (10e9 + 7)));}public void change(ArrayList<Integer> input, int max) {max += 1;if (max < 2) max = 2;for (int i = max; i <= 16; i++) {// 之前问题出在这,单纯long的话,他整溢出会超过long的范围double result = 0;int count = 0;for (int j = input.size() - 1; j >= 0; j--) {result += input.get(j) * Math.pow((double) i, (double) count);count += 1;}System.out.println((int) (result % (10e9+7)));}}
pdd:等差放置颜色鹅卵石




import java.util.*;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);int n=in.nextInt();// 存储鹅卵石颜色int[] colors=new int[n];for(int i=0;i<n;i++){colors[i]=in.nextInt();}// 存储每一个颜色的位置数组HashMap<Integer,ArrayList<Integer>> map=new HashMap<>();for(int i=0;i<n;i++){if(map.containsKey(colors[i])){map.get(colors[i]).add(i);}else{ArrayList<Integer> list=new ArrayList<>();list.add(i);map.put(colors[i],list);}}TreeMap<Integer,Integer> nowMap=new TreeMap<>();boolean panduan=false;// 遍历所有颜色,查看是否是等差数列,并求其等差for (Object o : map.keySet()) {ArrayList<Integer> cur=map.get(o);if(cur.size()==1){nowMap.put((Integer)o,0);}else{int count=cur.get(1)-cur.get(0);for(int i=2;i<cur.size();i++){if(cur.get(i)-cur.get(i-1)!=count) panduan=true;}if(panduan==false){nowMap.put((Integer)o,count);}panduan=false;}}System.out.println(nowMap.size());for (Object o : nowMap.keySet()) {System.out.println(o+" "+nowMap.get(o));}}}
937. 重新排列日志文件
这一题首先就是将数字类型的日志文件全部取出来,然后自定义一个排序的关系即可
pub fn reorder_log_files(logs: Vec<String>) -> Vec<String> {
let (mut dig, mut alpha) = (Vec::new(), Vec::new());
logs.into_iter().for_each(|s| {
// 只需要看最后一个字符是否是数字即可
let ss = s.chars().collect::<Vec<char>>();
let last = ss[ss.len()-1];
if last.is_ascii_digit() {
dig.push(s);
}else { alpha.push(s); }
});
alpha.sort_by(|a, b| {
let o1 = a.split_once(" ").unwrap();
let o2 = b.split_once(" ").unwrap();
if o1.1 == o2.1 {
a.cmp(b)
}else {
o1.1.cmp(o2.1)
}
});
alpha.extend(dig.into_iter());
alpha
}
