九度OJ--Q1473,九度oj

來源:互聯網
上載者:User

九度OJ--Q1473,九度oj

import java.util.ArrayList;
import java.util.Scanner;

/*
* 題目描述:
* 大家都知道,資料在電腦裡中儲存是以二進位的形式儲存的。
* 有一天,小明學了C語言之後,他想知道一個類型為unsigned int 類型的數字,儲存在電腦中的二進位串是什麼樣子的。
* 你能幫幫小明嗎?並且,小明不想要二進位串中前面的沒有意義的0串,即要去掉前置0。
* 輸入:
* 第一行,一個數字T(T<=1000),表示下面要求的數位個數。
* 接下來有T行,每行有一個數字n(0<=n<=10^8),表示要求的二進位串。
* 輸出:
* 輸出共T行。每行輸出求得的二進位串。
* 範例輸入:
* 5
* 23
* 535
* 2624
* 56275
* 989835
* 範例輸出:
* 10111
* 1000010111
* 101001000000
* 1101101111010011
* 11110001101010001011
*/

public class q1473 {

  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    int T = scanner.nextInt();
    int[] pre = new int[T];
    for(int m=0; m<T; m++) {
      pre[m] = scanner.nextInt();
    }

    for(int m=0; m<T; m++) {

      int n = pre[m];
      ArrayList<Integer> a = new ArrayList<Integer>();
      

      // 考慮輸入整數為0的情況
      if(n == 0) {
        System.out.println("0");
        continue;
      }
      else {
        while(n!=0) {

          a.add(n%2);
          n = n / 2;

        }

        Integer target[] = new Integer[a.size()];

        target = a.toArray(target);

        for(int i=target.length-1; i>=0; i--) {
          System.out.print(target[i]);
        }
        System.out.println();
      }
    }

    scanner.close();

  }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.