【快手笔试题解】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);
}
}