/ 猿问

# 请教一下各位，JAVA 实现算法？

2019-11-21 17:15:24

# JAVA 实现算法

## 3 回答

HUWWW

public class Test{
static public int getIntegerComplement(int n){
}
public static void main(String[] args){
int a[]={1,5,50,256,65536};
for(int i:a){
int r=getIntegerComplement(i);
System.out.println(i+" "+Integer.toBinaryString(i)+
" => "+r+" "+Integer.toBinaryString(r));
}
}
}
========
1 1 => 0 0
5 101 => 2 10
50 110010 => 13 1101
256 100000000 => 255 11111111
65536 10000000000000000 => 65535 1111111111111111

public class Convert {
public static void main(String[] args) {
for (int N = 0; N <= 10; N++) {
int value = getIntegerComplement(N);
System.out.print(N + "(" + toBinaryString(N) + ") -> ");
System.out.println(value + "(" + toBinaryString(value) + ")");
}
}
public static int getIntegerComplement(int n) {
int len = toBinaryString(n).length();
return Integer.parseInt(toBinaryString(~n).substring(32 - len), 2);
}
public static String toBinaryString(int n) {
return Integer.toBinaryString(n);
}
}

“Java的标准库'中用了一些常用数据结构和相关算法.

• 3 回答
• 0 关注
• 464 浏览

0/150