计算十进制整数在二进制中是几位数

 手算过程:
n除以二,求余数,n为0停下,余数逆序排列,解决问题

计算十进制整数在二进制中是几位数_第1张图片

 代码实现:
 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)3000 + 10;

    public static void main(String[] args ) throws IOException, ParseException
    {
        int n = 2021;
        int cnt = 0;
        while(n > 0)
        {
            n /= 2;
            cnt ++;
        }
        pw.println(cnt);
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException  { return reader.readLine(); }

    static String next() throws IOException
    {
        while (!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException  { return Integer.parseInt(next()); }

    static double nextDouble() throws IOException { return Double.parseDouble(next()); }

    static long nextLong() throws IOException  { return Long.parseLong(next());}

    static BigInteger nextBigInteger() throws IOException { return new BigInteger(rd.nextLine()); }
}

class PII
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
}

你可能感兴趣的:(java,小技巧,蓝桥杯,java,jvm,蓝桥杯)