华为题,放芯片

假设有n块芯片,负责AB两种业务。其中,每块芯片只能是运行4个A业务,或者1个B业务。
其中,不可以既执行A业务,由执行B业务,即二者是独立的
输入为三行,第一行芯片数量,第二行业务数量
第三行A B A B A这种描述各种业务的
大佬的思路,拜读

  1. // 输入为芯片数量以及业务集合
  2. public void compute1(int nums_core, String server){
  3. // 分别记录装满的芯片id,可以引申为当前B装配的芯片ID
  4. // curr_id记录当前没有装满的id,引申为当前A放置的芯片
  5. // curr_opt指A放置的芯片中,目前放置在第几位
  6. int full_id = 0, curr_id = 0, curr_opt = 0;
  7. char opt = ' ';
  8. for (int i = 0; i< server.length(); i++) {
  9. // 获取当前元素
  10. opt = server.charAt(i);
  11. if (opt == 'A') {
  12. // 如果当前芯片没有存储的话,初始化的作用
  13. if (curr_opt == 0){
  14. curr_id = full_id +1;
  15. }
  16. // 如果是正常情况,在芯片中的编号++
  17. curr_opt++;
  18. // 否则需要另开一块芯片,只能放置在放满的后面
  19. if (curr_opt == 5) {
  20. curr_id = full_id + 1;
  21. curr_opt = 1;
  22. }
  23. full_id = Math.max(full_id, curr_id);
  24. }else {
  25. full_id++;
  26. }
  27. }
  28. if (Math.max(full_id, curr_id) > nums_core) {
  29. System.out.println(0);
  30. System.out.println(0);
  31. }else if (opt == 'B'){
  32. System.out.println(full_id);
  33. System.out.println(1);
  34. }else{
  35. System.out.println(curr_id);
  36. System.out.println(curr_opt);
  37. }
  38. }

进制转换

阿里题,对于输入的一个数,不知道具体是什么进制的,那么就按照2~16进制将其对10进制进行转换,然后输出。输出的数据很大,需要对10e9+7取模

  1. public static void main(String[] args) {
  2. Scanner scanner = new Scanner(System.in);
  3. Main main = new Main();
  4. String next = scanner.next();
  5. char[] array = next.toCharArray();
  6. ArrayList<Integer> input = new ArrayList<>(array.length);
  7. for (char c : array) {
  8. if (c > '0' && c < '9')
  9. input.add(c - '0');
  10. else
  11. input.add(c - 'A' + 10);
  12. }
  13. Integer max = input.stream().max((o1, o2) -> o1 - o2).get();
  14. main.change(input, max);
  15. System.out.println((int) (1.004336277661869e+59 % (10e9 + 7)));
  16. }
  17. public void change(ArrayList<Integer> input, int max) {
  18. max += 1;
  19. if (max < 2) max = 2;
  20. for (int i = max; i <= 16; i++) {
  21. // 之前问题出在这,单纯long的话,他整溢出会超过long的范围
  22. double result = 0;
  23. int count = 0;
  24. for (int j = input.size() - 1; j >= 0; j--) {
  25. result += input.get(j) * Math.pow((double) i, (double) count);
  26. count += 1;
  27. }
  28. System.out.println((int) (result % (10e9+7)));
  29. }
  30. }

pdd:等差放置颜色鹅卵石

image.png
image.png
image.png
image.png

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. int n=in.nextInt();
  6. // 存储鹅卵石颜色
  7. int[] colors=new int[n];
  8. for(int i=0;i<n;i++){
  9. colors[i]=in.nextInt();
  10. }
  11. // 存储每一个颜色的位置数组
  12. HashMap<Integer,ArrayList<Integer>> map=new HashMap<>();
  13. for(int i=0;i<n;i++){
  14. if(map.containsKey(colors[i])){
  15. map.get(colors[i]).add(i);
  16. }
  17. else{
  18. ArrayList<Integer> list=new ArrayList<>();
  19. list.add(i);
  20. map.put(colors[i],list);
  21. }
  22. }
  23. TreeMap<Integer,Integer> nowMap=new TreeMap<>();
  24. boolean panduan=false;
  25. // 遍历所有颜色,查看是否是等差数列,并求其等差
  26. for (Object o : map.keySet()) {
  27. ArrayList<Integer> cur=map.get(o);
  28. if(cur.size()==1){
  29. nowMap.put((Integer)o,0);
  30. }
  31. else{
  32. int count=cur.get(1)-cur.get(0);
  33. for(int i=2;i<cur.size();i++){
  34. if(cur.get(i)-cur.get(i-1)!=count) panduan=true;
  35. }
  36. if(panduan==false){
  37. nowMap.put((Integer)o,count);
  38. }
  39. panduan=false;
  40. }
  41. }
  42. System.out.println(nowMap.size());
  43. for (Object o : nowMap.keySet()) {
  44. System.out.println(o+" "+nowMap.get(o));
  45. }
  46. }
  47. }

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
}