首页 > 代码库 > Java练习题4_last_1042林大锐格平台

Java练习题4_last_1042林大锐格平台

import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        int ri , repeat;
        int  n;
        Scanner in = new Scanner(System.in);
        repeat = in.nextInt();
        for(ri = 1 ; ri <= repeat ; ri++){
            n=in.nextInt();      
            dectobin(n);
        }
    }
    static void dectobin(int n){
    	int tmp=n;
    	int []a=new int[100];
    	a[0]=0;
    	int k=0;
    	while(true){
    		if(tmp==0){
    			break;
    		}
    		else
    		{
    			a[k]=tmp%2;
    			k++;
    			tmp/=2;
    		}
    	}
    	if(n!=0){
    		int stop=a.length-1;
    	while(true){if(a[stop]!=0)break;else stop--;}
    	for(int i=stop;i>=0;i--)
    		System.out.printf("%d", a[i]);
    	}
    	else
    		System.out.println("0");
    	System.out.println();
    }
    //write your own codes
    
    
}

 

Java练习题4_last_1042林大锐格平台