第一种:除基倒取余法 这是最符合我们平时的数学逻辑思维的,即输入一个十进制数n,每次用n除以2,把余数记下来,再用商去除以2...依次循环,直到商为0结束,把余数倒着依次排列,就构成了转换后的二进制数. 那么,在实际实现中,可以用int的一个数来存储最后的二进制,每次求余后把余数存储在int型数的低位,依次递增. public void binaryToDecimal(int n){ int t = 0; //用来记录位数 int bin = 0; //用来记录最后的二进制数 int r = 0
1282 - Leading and Trailing You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk. Input Input starts with an integer T (≤ 1000), denoting the number of test cases. Each
闲着没事写了个简单的十进制转二进制的算法,很简单,个人记录一下,不妥之处请指正. public static String toBinaryString(int j) { if (j < 0) { throw new NumberFormatException("不支持负数"); } double i = (double) j; StringBuilder sb = new StringBuilder(); while (true) { if (i % 2 == 0) sb.ap
自从JDK7更新之后,新增了二进制变量的表示,支持将整数类型用二进制来表示,用0b开头: 例如: byte b= (byte) 0b1000_0001; short s = (short) 0b1000_0000_0000_0001; 新手在这个时候会遇到一个问题,为啥byte b=0b100_00001会报错(cannot convert from int to byte) 而short b=0b100_00001又不会呢?既然java底层默认0b100_00001是int类型,为什么shor
考验理解能力的时候到了 T^T Very often, especially in programming contests, we treat a sequence of non-whitespace characters as a string. But sometimes, a string may contain whitespace characters or even be empty. We can have such strings quoted and escaped to h