【快手笔试题解】T1 - 80% T2-100% T3-100%

//测试样例没过  样例给的树没看明白
//10,5,15,3,7,13,18
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class T1 {
    static int flag = Integer.MIN_VALUE;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        if (str.isEmpty()) {
            System.out.println("True");
        }
        String[] strs = str.split(",");
        Node node = init(strs);
        System.out.println(isTree(node)== true?"True":"False");
    }

    private static Node init(String[] str) {
        if (str.length == 1) {
            return new Node(str[0].charAt(0));
        }
        List<Node> list = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            list.add(new Node(str[i].charAt(0)));
        }
        int tmp = 0;
        while (tmp <= (str.length - 2) / 2) {
            if (2 * tmp + 1 <= str.length) {
                list.get(tmp).left = list.get(2 * tmp + 1);
            }
            if (2 * tmp + 2 <= str.length) {
                list.get(tmp).right = list.get(2 * tmp + 2);
            }
            tmp++;
        }
        return list.get(0);
    }

    private static boolean isTree(Node node) {
        if (node == null) {
            return true;
        }
        boolean x = isTree(node.left);
        if (node.data >= flag && x) {
            flag = node.data;
        } else {
            return false;
        }
        boolean y = isTree(node.right);
        return y;
    }
}
class Node{
    int data;
    Node left;
    Node right;

    public Node(int data, Node left, Node right) {
        this.data = data;
        this.left = left;
        this.right = right;
    }

    public Node(int data) {
        this.data = data;
    }

}


import java.util.Scanner;

public class T2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a, b, c;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        boolean[][] flag = new boolean[a][b];
        System.out.println(isvist(a, b, c, 0, 0,flag));
    }

    //判断上下左右 是否可以访问
    private static int isvist(int a, int b, int c, int x, int y, boolean[][] flag){
        if (x < 0 || y < 0 || x >= a || y >= b || flag[x][y] == true || fun(x) + fun(y) > c) {
            return 0;
        }
        flag[x][y] = true;
        return 1 + isvist(a, b , c, x - 1, y, flag) + isvist(a, b , c, x , y - 1, flag)
                +isvist(a, b , c, x + 1, y, flag) + isvist(a, b , c , x, y + 1, flag);
    }

    //取各个位数
    private static int fun(int x){
        int sum = 0;
        do {
            sum+= x % 10;
            x = x / 10;

        }while (x > 0);
        return sum;
    }

}

 




import java.math.BigInteger;
import java.util.Scanner;

public class T3 {
    public static void main(String[] args) {
        int count = 0;
        Scanner sc = new Scanner(System.in);
        String n = sc.next();

        BigInteger bigInteger = new BigInteger(n);
        String s = bigInteger.toString( 2);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '1'){
                count++;
            }
        }
        System.out.println(count);
    }
}

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值