腾讯笔试 2021.9.5

第一题  AC

package cn.s.test.TENCENT0905;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


class ListNode {
   int val;
   ListNode next = null;
   public ListNode(int val) {
     this.val = val;
   }
 }

public class TEST_1 {
    public static void main(String[] args) {

    }
    public ListNode solve (ListNode[] a) {
        // write code here
        ListNode dummy = new ListNode(-1);
        //int length = a.length;
        if (a.length == 0){
            return dummy.next;
        }
        int[] nums = new int[a.length];
        ListNode node = new ListNode(-1);
        dummy.next = node;
        int sum = 0;
        while (sum != a.length){
            for (int i = 0; i < a.length; i++) {
                if (a[i] != null){
                    ListNode tempNode = new ListNode(a[i].val);
                    node.next = tempNode;
                    node = node.next;
                    a[i] = a[i].next;
                }
                if (a[i] == null && nums[i] == 0) {
                    nums[i] = 1;
                }
            }
            sum = 0;
            for (int i = 0; i < a.length; i++) {
                if (nums[i] == 1){
                    sum++;
                }
            }
        }
        return dummy.next.next;
    }
}

 第二题    AC

package cn.s.test.TENCENT0905;

import java.util.Arrays;
import java.util.Scanner;

public class TEST_2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums_A = new int[n];
        int[] nums_B = new int[n];
        int i = 0;
        while (i < n) {
            int temp = sc.nextInt();
            nums_A[i++] = computer1(temp);
        }
        Arrays.sort(nums_A);
        i = 0;
        while (i < n) {
            int temp = sc.nextInt();
            nums_B[i++] = computer1(temp);
        }
        Arrays.sort(nums_B);
        System.out.println(solve(nums_A, nums_B, 0));
    }

    private static int solve(int[] nums_a, int[] nums_b, int ans) {
        int a = nums_a.length - 1, b = nums_b.length - 1;
        while (a >= 0 && b >= 0) {
            if (nums_a[a] > nums_b[b]) {
                ans++;
                a--;
                b--;
            } else {
                b--;
            }
        }
        return ans;
    }

    private static int computer1(int num) {
        int sum = 2;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                if (i == Math.sqrt(num) && num / i == i) {
                    sum++;
                } else sum += 2;
            }
        }
        return sum;
    }

    private static int computer(int num) {
        int sum = 0;
        for (int i = 1; i < num; i++) {
            if (sum % i == 0)
                sum++;
        }
        return sum;
    }
}

 第三题    42.15

package cn.s.test.TENCENT0905;

import java.util.Scanner;

public class TEST_3 {
    public static int allsum = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        String str = sc.nextLine();
        char[] nums = str.toCharArray();
        search(nums,0,0,'2',0);
        System.out.println(allsum);
    }

    private static void search(char[] nums, int index, int curVal, char c, int val) {
        if (index == nums.length){
            if (curVal > allsum){
                allsum = curVal;
                return;
            }
            return;
        }
        int tmpdif = nums.length - index;
        if (allsum - curVal >= ((val*2 + 1 + tmpdif) * tmpdif / 2)){
            return;
        }
        search(nums, index + 1, curVal, c, val);
        int temp = index + 1, temp_cur = curVal + 1;
        if (nums[index] == c){
            search(nums, temp, temp_cur+ val, c , val + 1);
        }else {
            search(nums, temp, temp_cur, nums[index], 1 );
        }
    }
}

 第四题    30

package cn.s.test.TENCENT0905;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TEST_41 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextInt();
        long l = sc.nextInt();
        long r = sc.nextInt();
        //int first = n / 2;int second = n % 2;
        List<Long> list = new ArrayList<>();
        list.add(n);
        boolean bool = false;
        while(list.size() != 0){
            if (bool == true || list.size() > 2 * r){
                break;
            }
            bool = true;
            List<Long> list1 = new ArrayList<>(list);
            List<Long> list2 = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                long data = list1.get(i);
                if (data <= 1){
                    list2.add(data);
                }else {
                    long x = data /2;
                    long y = data% 2;
                    if (x > 1 || y > 1){
                        bool = false;
                    }
                    list2.add(x);
                    list2.add(y);
                    list2.add(x);
                }
            }
            list.clear();
            list = list2;
        }
        long ans = 0;
        for (int i = (int)l - 1; i < (int)r; i++) {
            if (list.get(i) == 1) ans++;
        }
        System.out.println(ans);
    }
}

  

 第五题   40

package cn.s.test.TENCENT0905;

import java.util.Scanner;

public class TEST_5 {
    public static void main(String[] args) {
        int sum = 0, index = 0;
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        int i = 0;
        while (i < n){
            nums[i++] = sc.nextInt();
        }
        for (int j = 0; j < n; j++) {
            for (int t = j + 1; t < n; t++) {
                if ( t == j + 1){
                    sum++;
                    continue;
                }else {
                    index = j +1;
                    while (index < t && nums[index] >= nums[j] && nums[index] >= nums[t] ){
                        index++;
                    }
                    if (index == t){
                        sum++;
                    }
                }
            }
        }
        System.out.println(sum);
    }
}

  

posted @ 2021-09-05 22:18  被梦想尘封的时光  阅读(81)  评论(0编辑  收藏  举报