今日头条

问题二

今日头条_第1张图片
so easy,直接把棍子排序,然后找最大的
看最大的是否小于其他加和
注意边界:棍子数<=2,棍子长度为负

代码AC

package ac;

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


public class Main {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<Integer> sticks = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            int op = in.nextInt();
            int content = in.nextInt();
            if (op == 1) {
                sticks.add(content);
                Collections.sort(sticks);
            } else if (op == 2) {
                for (int j = 0; j < sticks.size(); j++) {
                    if (sticks.get(j) == content) {
                        sticks.remove(j);
                        break;
                    }
                }
                Collections.sort(sticks);
            }
            if (sticks.size() <= 2) {
                System.out.println("No");
            } else if (sticks.get(0) < 0) {
                System.out.println("No");
            } else {
                int sum = 0;
                int max = sticks.size() - 1;
                int maxStick = sticks.get(max);
                for (int j = 0; j < max; j++) {
                    sum += sticks.get(j);
                }
                if (maxStick >= sum) {
                    System.out.println("No");
                } else {
                    System.out.println("Yes");
                }
            }

        }
    }
}

问题三

今日头条_第2张图片

代码

没做

问题一

今日头条_第3张图片
用HashMap

ABC : A => 100, B => 10, C => 1
BCA : B => 100, C => 10, A => 1
合并两个就是 :A => 101, B => 110, C => 11
所以应该分给B => 9 ,A => 8, C => 7

需要考虑的是头不能为0,所以需要判断不能为0的项。
另外要注意,需要全部用long,不用int,这个吃亏吃大了

代码90%A

仓促做的,比较乱,需要重新整理了

package first;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class TestUtils {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        String bC = in.nextLine();

        int n = Integer.valueOf(bC);
        HashMap<Character, Long> ci = new HashMap<Character, Long>();
        HashSet<Character> nzero = new HashSet<Character>();
        List<Long> co = new ArrayList<Long>();

        for (int i = 0; i < n; i++) {
            String content = in.nextLine();
            int quan = content.length() - 1;
            for (int j = 0; j < content.length(); j++) {
                char ch = content.charAt(j);
                if (j == 0) {
                    nzero.add(ch);
                }

                long sumQuan = (long) Math.pow(10, quan);
                if (ci.get(ch) == null) {
                    ci.put(ch, sumQuan);
                } else {
                    long tempQuan = ci.get(ch);
                    tempQuan += sumQuan;
                    ci.put(ch, tempQuan);
                }
                quan--;
            }
        }

        for (Map.Entry<Character, Long> en : ci.entrySet()) {
            long tempQuan = en.getValue();
            co.add(tempQuan);
        }

        Collections.sort(co);


        List<Long> nonz = new ArrayList<Long>();
        long zeroChC = 0;
        if (co.size() == 10) {
            for (Map.Entry<Character, Long> en : ci.entrySet()) {
                if (!nzero.contains(en.getKey())) {
                    nonz.add(en.getValue());
                }
            }

            Collections.sort(nonz);

            if (nonz.size() == 0) {
                nonz.add(new Long(0));
// System.out.println("0");
// return;
            }
            zeroChC = nonz.get(0);
        }

        long sum = 0;

        int size = co.size();
        int begin = 9 - size + 1;
        if (size == 10) {
            begin = 1;
            for (Long item : co) {
                if (item == zeroChC) {
                    sum += 0;
                } else {
                    sum += item * begin;
                    begin++;
                }
            }
        } else {
            for (Long item : co) {
                sum += item * begin;
                begin++;
            }
        }

        System.out.println(sum);
    }
}

问题四

今日头条_第4张图片
so easy啊:

5 => 101
2 => 010
看出规律没?
85 => 1010101
2 =>  0000010
A | B = A + B 等价于在A1的位,B一定为0
所以我们只需要找第k个,把k二进制化,对应位填到A0的位,从小到大。
注意,B可能超过A,所以在超过A后,A的所有高位都是0,需要注意
还有,也要全用long,不用int

代码AC

package third;

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

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
// System.out.print(Long.MAX_VALUE);
        while (true) {
            int x = in.nextInt();
            int k = in.nextInt();

            List<Integer> p2not1 = new ArrayList<Integer>();

            // 二进制位数
            int p2 = 0;
            int xt = x;
            while (xt != 0) {
                if (xt % 2 == 0) {
                    p2not1.add(p2);
                }
                xt /= 2;
                p2++;
            }

            int kt = k;
            int k2 = 0;
            long ax = 0;

            while (kt != 0) {
                if (kt % 2 != 0) {
                    int pow = 0;
                    if (k2 <= p2not1.size() - 1) {
                        pow = p2not1.get(k2);
                    } else {
                        pow = p2 + k2 - (p2not1.size());
                    }
                    ax += pow(2, pow);
                }

                k2++;
                kt /= 2;
            }
            System.out.println(ax);
        }
    }

    public static long pow(int di, int mi) {
        long re = 1;
        for (int i = 0; i < mi; i++) {
            re *= di;
        }
        return re;
    }
}

你可能感兴趣的:(今日头条)