腾讯实习生笔试题

public class StringDemo2 {
    public static void main(String args []) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        int m = sc.nextInt();
        System.out.println((n / (m << 1)) * (m * m));
    }
}

2

public class StringDemo3{
    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
        int n =in.nextInt();
        long[] a=new long [100];
        long x=0,y=0;
        int i,j;
        for ( i=0;i<n;i++)
            a[i]=in.nextLong();

        for( i=0;i<n;i++){//外层循环控制排序趟数
            for( j=0;j<n-1-i;j++){//内层循环控制每一趟排序多少次
                if(a[j]<a[j+1]){
                    long temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        //System.out.println("排序后如下");
        //for ( i=0;i<n;i++)
        //System.out.println(a[i]);
        for (i=1;i<=n/2;i++){
            x=x+a[i*2];
            y=y+a[i*2-1];
        }
        x=x+a[0];
        System.out.println(x-y);
    }
}

/*
小Q的父母要出差N天,走之前给小Q留下了M块巧克力。
小Q决定每天吃的巧克力数量不少于前一天吃的一半,
但是他又不想在父母回来之前的某一天没有巧克力吃,
请问他第一天最多能吃多少块巧克力
 */
public class StringDemo4{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int day = scan.nextInt();
        int number = scan.nextInt();
        System.out.print(fun(day,number));
    }

    //计算第一天吃s个巧克力一共需要多少个巧克力
    public static int sum(int s, int n, int m){
        int sum=0;
        for(int i=0;i<n;i++){
            sum+=s;
            s=(s + 1)/2;//向上取整
        }
        return sum;
    }
    //二分查找
    public static int fun(int n, int m){
        if(n==1) return m;
        int low=1;
        int high=m;//第一天的巧克力一定是大于等于1小于等于m的
        while(low<=high){
            int mid=(low+high + 1)/2;//向上取整
            if(sum(mid, n, m)==m) return mid;//如果第一天吃mid个巧克力,刚刚好吃完所有巧克力,那么直接返回
            else if(sum(mid, n, m)<m){
                low=mid+1;
            }else{
                high=mid-1;
            }
        }
        return high;
    }
}

public class StringDemo5{
    public static final int ASD = 1000000007;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();
        int a=sc.nextInt(), x=sc.nextInt();
        int b=sc.nextInt(), y=sc.nextInt();
        int[] dp = new int[k+1];
        dp[0] = 1;
        for(int i=0; i<x ; i++){
            for(int j=k; j>=a; j--){
                dp[j] = (dp[j] + dp[j-a]) % ASD;
            }
        }

        for(int i=0; i<y ; i++){
            for(int j=k; j>=b; j--){
                dp[j] = (dp[j] + dp[j-b]) % ASD;
            }
        }

        System.out.println(dp[k]);
        sc.close();
    }
}

5

public class StringDemo6 {

    static class Pair {
        int time;
        int diff;

        public Pair(int time, int diff) {
            this.time = time;
            this.diff = diff;
        }
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int machineNum = sc.nextInt();
            int taskNum = sc.nextInt();
            Pair[] machines = new Pair[machineNum];
            Pair[] tasks = new Pair[taskNum];
            for (int i = 0; i < machineNum; i++) {
                machines[i] = new Pair(sc.nextInt(), sc.nextInt());
            }
            for (int i = 0; i < taskNum; i++) {
                tasks[i] = new Pair(sc.nextInt(), sc.nextInt());
            }
            Comparator<Pair> cmp = new Comparator<Pair>() {

              @Override
                public int compare(Pair p1, Pair p2) {
                    if (p1.time == p2.time) {
                        return p2.diff - p1.diff;
                    } else {
                        return p2.time - p1.time;
                    }
                }

            };
            Arrays.sort(machines, cmp);
            Arrays.sort(tasks, cmp);
            long sum = 0;
            int count = 0;
            int j = 0;
            int[] dp = new int[101];
            for (int i = 0; i < taskNum; i++) {
                while (j < machineNum && machines[j].time >= tasks[i].time) {
                    dp[machines[j].diff]++;
                    j++;
                }
                for (int k = tasks[i].diff; k < 101; k++) {
                    if (dp[k] != 0) {
                        dp[k]--;
                        sum += 200 * tasks[i].time + 3 * tasks[i].diff;
                        count++;
                        break;
                    }
                }
            }
            System.out.println(count + " " + sum);
        }
        sc.close();

    }

}

public class main {

        private String[][] str;
        private int N;
        private int M;

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            main test = new main();
            String inputStr = null;
            Scanner in = new Scanner(System.in);
            test.N = in.nextInt();
            test.M = in.nextInt();
            int cN = test.N;
            int cM = test.M;
            test.str = new String[cN][cM];
            for(int i = 0; i < cN; i++){
                if(in.hasNext())
                    inputStr = in.next();
                for(int j = 0; j < cM; j++){
                    test.str[i][j] = String.valueOf(inputStr.charAt(j));
                }
            }


            int num = test.getLeastOperation(test.str,cN,cM);

            System.out.println(num);

        }

        private int getLeastOperation(String[][] str, int n, int m){
            int num = 0;
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    if("Y".equals(str[i][j])){
                        clear_Y(i,j);     //消除Y
                        num++;
                    }else if("B".equals(str[i][j])){
                        clear_B(i,j);     //消除B
                        num++;
                    }else if("G".equals(str[i][j])){
                        clear_Y(i,j);     //消除Y
                        num++;
                        clear_B(i,j);     //消除B
                        num++;
                    }
                }
            }
            return num;
        }

        private void clear_Y(int i, int j){
            for(; j < M && i < N; j++, i++){
                if("Y".equals(str[i][j]))
                    str[i][j] = "X";
                else if("G".equals(str[i][j]))
                    str[i][j] = "B";
                else
                    break;
            }
        }

        private void clear_B(int i, int j){
            for(; j >= 0 && i < N; j--,i++){
                if("B".equals(str[i][j]))
                    str[i][j] = "X";
                else if("G".equals(str[i][j]))
                    str[i][j] = "Y";
                else
                    break;
            }
        }

    }

public class TestDeadLock implements Runnable{
    public int flag =1;
    static Object o1 = new Object();
    static Object o2 = new Object();

    public void run(){
        System.out.println("flag=" +flag);
        if(flag == 1){
            synchronized(o1){
                try{
                    Thread.sleep(500);
                }catch(Exception e){
                    e.printStackTrace();
                }
                synchronized(o2){
                    System.out.println("1");
                }
            }
        }
        if(flag == 0){
            synchronized(o2){
                try{
                    Thread.sleep(500);
                }catch(Exception e){
                    e.printStackTrace();
                }
                synchronized(o1){
                    System.out.println("0");
                }
            }
        }
    }
    public static void main(String[] args){
        TestDeadLock td1 = new TestDeadLock();
        TestDeadLock td2 = new TestDeadLock();
        td1.flag = 1;
        td2.flag = 0;
        Thread t1 = new Thread(td1);
        Thread t2 = new Thread(td2);
        t1.start();
        t2.start();
    }
}

public class BlockTest  {

    public static void main(String[] args) {
        final ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<3;i++){
                    queue.add(i);
                }
            }
        });
        try {
            thread.join();
            thread.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i=0;i<15;i++){
            queue.take();
        }
    }

}

class ArrayBlockingQueue<T> {
    private Lock lock = new ReentrantLock();
    private Object[] item = new Object[4];
    private int startIndex,endIndex,count;
    private Condition notfull = lock.newCondition();
    private Condition notempty = lock.newCondition();
    public void add(T t){
        lock.lock();
        try{
            System.out.println("存放值"+t);
            while(count==item.length){
                try {
                    System.out.println("队列已满,阻塞put线程");
                    notfull.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            item[startIndex++] = t;
            count++;
            if(startIndex==item.length){
                startIndex=0;
            }
            notempty.signal();
        }finally{
            lock.unlock();
        }
    }

    public T take(){
        lock.lock();
        try{
            while(count==0){
                try {
                    System.out.println("队列空了,阻塞take线程");
                    notempty.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t =(T)item[endIndex++];
            System.out.println("取值"+t);
            count--;
            if(endIndex==item.length){
                endIndex = 0;
            }
            notfull.signal();
            return t;
        }finally{
            lock.unlock();
        }
    }
}

XSS就是通过在css或者html里嵌入一些恶意的Javascript代码,从而实现恶意攻击的目的。
在编写前端页面时,注意不要把任何来自用户的内容放到下面这些地方
script标签、html注释、标签名、属性、css值、
防范方法:
1、转义用户的内容 如对
a、HTML:对以下这些字符进行转义:
&:&amp; <:&alt;  >:&gt;  ':&#x27; ":&quot;  /:&#x2F;
b、Javascript:把所有非字母、数字的字符都转义成小于256的ASCII字符;
c、URL:使用Javascript的encodeURIComponent()方法对用户的输入进行编码,该方法会编码如下字符:,      /      ?     :     @     &     =     +     $     #
2、添加用户生成的内容
a、Javascript:使用textContent或者innerText,而不能使用innerHTML;
b、jquery:使用text(),而不能使用html();

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个IP地址(例如168.0.0.1)");
        String ss = sc.next();
        String[] split = ss.split("\\.");
        for (String s1 : split) {
            int i = Integer.parseInt(s1);/
            System.out.print(i);
        }
    }
}

public class CardsTest {

    public static void main(String[] args) {

        Cards cards = new Cards();

        Thread t1 = new Thread(cards);

        t1.start();
    }

}

class Cards implements Runnable {

    private Map<Integer, String> map = new HashMap<Integer, String>();
    private List<Integer> list = new ArrayList<Integer>();
    private TreeSet<Integer> mike = new TreeSet<Integer>();
    private TreeSet<Integer> qq = new TreeSet<Integer>();
    private TreeSet<Integer> john = new TreeSet<Integer>();
    private TreeSet<Integer> dipai = new TreeSet<Integer>();

    public void shuffle() {// 发牌
        String[] color = {"红桃", "黑桃", "梅花", "方块"};
        String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
                "Q", "K", "A"};
        int index = 0;
        for (String num : numbers) {
            for (String cc : color) {
                String concat = cc.concat(num);
                map.put(index, concat);
                list.add(index);
                index++;
            }
        }
        map.put(index, "小王");
        list.add(index);
        index++;
        map.put(index, "大王");
        list.add(index);
        Collections.shuffle(list);
        for (int i = 0; i < list.size(); i++) {
            if (i >= list.size() - 3) {
                dipai.add(list.get(i));
            } else if (i % 3 == 1) {
                qq.add(list.get(i));
            } else if (i % 3 == 2) {
                john.add(list.get(i));
            } else if (i % 3 == 0) {
                mike.add(list.get(i));
            }
        }
    }

    public void lookPuker(String name, TreeSet<Integer> ts,
                          Map<Integer, String> hm) {
        System.out.println(name + "共有 " + ts.size() + " 你的牌是:");
        for (Integer it : ts) {
            String value = hm.get(it);
            System.out.print(value + " ");
        }
        System.out.println();
    }

    @Override
    public void run() {
        shuffle();
        int a = (int) (Math.random() * 3 + 1);
        if (a == 1) {
            System.out.println("地主 张三");
            lookPuker("张三", mike, map);
            lookPuker("李四", qq, map);
            lookPuker("王五", john, map);
            lookPuker("底牌", dipai, map);
        } else if (a == 2) {
            System.out.println("地主 李四");
            lookPuker("李四", qq, map);
            lookPuker("张三", mike, map);
            lookPuker("王五", john, map);
            lookPuker("底牌", dipai, map);
        } else if (a == 3) {
            System.out.println("地主 王五");
            lookPuker("王五", john, map);
            lookPuker("李四", qq, map);
            lookPuker("张三", mike, map);
            lookPuker("底牌", dipai, map);
        }
    }
}

发生原因有如下情况:
1.可能是mysql.exe 的cpu占用率过高,
2.可能是其中一个进程特别耗CPU内存

定位:
首先输入top,查看占用资源较多的线程,接着使用jstack工具查看堆栈信息,根据堆栈信息来做判断
如果是mysql.exe情况  使用show processlist命令查找最频繁的sql语句 ,修改数据的参数设置,where条件后添加索引进行sql优化
如果在遇到上述的问题 就扩大内存,然后在解决相应的问题。

猜你喜欢

转载自blog.csdn.net/weixin_38201936/article/details/88836824