[java]代码库
import java.util.Arrays;
import java.util.Scanner;
public class binary{
public static void main(String[]args){
Scanner in = new Scanner(System.in);
System.out.print("确认输入的数字个数:q =");
int q = in.nextInt();
int arr[] = new int[q];
for(int w = 0;w < q;w++){
System.out.print("arr["+(w+1)+"]=");
arr[w] = in.nextInt();
}
for(int w = 0;w < q;w++){
int i=0;
int r=0;
int n=arr[w];
int[] a =new int[16];
do{
if(n%2==1){
r=1;
}
else {
r=0;
}
a[i]=r;
n=n/2;
i++;
}
while(n>0);
if ( i<=a.length) {
for (int j = 0; j < a.length-i; j++) {
System.out.print("0"+",");
}
}
for (int j = i-1; j>=0; j--) {
System.out.print(a[j]+",");
}
}
}
}
[代码运行效果截图]
by: 发表于:2017-07-07 15:48:14 顶(0) | 踩(0) 回复
??
回复评论