将整数N用二进制表示,并转换为String
算法第四版作业1.1.9
自己写的tran1.官方答案tran2
package homework1_1;
/**
* @description: ${description}
* @create: 2019-02-03
**/
public class TenToTwo {
public static String tran2(int x){
String s="";
while(x>0){
int a=x%2;
s=a+s;
x=x/2;
}
return s;
}
public static String tran1(int x){
String s="";
for(int j=x;j>0;j=j/2){
s=(j%2)+s;
}
return s;
}
public static void main(String[] args) {
System.out.println(tran2(789));
System.out.println(tran1(789));
}
}
结果如下:
还没有评论,来说两句吧...