藍橋杯:基礎練習 01字串【JAVA演算法實現】

來源:互聯網
上載者:User

標籤:藍橋杯   基礎練習   01字串   java   演算法   

題目描述

對於長度為5位的一個01串,每一位都可能是0或1,一共有32種可能。它們的前幾個是:

00000
00001
00010
00011
00100

請按從小到大的順序輸出這32種01串。

輸入格式

本試題沒有輸入。

輸出格式

輸出32行,按從小到大的順序每行一個長度為5的01串。

範例輸出

00000
00001
00010
00011
00100

<以下部分省略>

public class Main {public static void main(String[] args) {//Scanner cin = new Scanner(System.in);int n1=0,n2=0,n3=0,n4=0,n5=0;for(int i=0;i<32;i++){if (i%2==0)n1=0;else n1=1;if (i/2%2==0)n2=0;else n2=1;if (i/4%2==0)n3=0;else n3=1;if (i/8%2==0)n4=0;else n4=1;if (i/16%2==0)n5=0;else n5=1;System.out.println(n5+""+n4+""+n3+""+n2+""+n1);}}}
<pre name="code" class="java">public class Main {public static void main(String[] args) {// Scanner cin = new Scanner(System.in);String[] n = new String[5];for (int i = 0; i < 32; i++) {int ii = i;for (int j = 0; j < 5; j++) {n[j]=ii%2+"";ii = ii / 2;}System.out.println(n[4]+n[3]+n[2]+n[1]+n[0]);}}}

 

藍橋杯:基礎練習 01字串【JAVA演算法實現】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.