01字串--java

來源:互聯網
上載者:User

標籤:test   進位   ring   01字串   其他   res   void   簡單   png   

題目如下:

   

解法一:簡單、討巧了

 1 class test  2 { 3     public static void main(String[] args)  4     { 5         int a,b,c,d,e; 6  7         for(a=0;a<=1;a++) 8           for(b=0;b<=1;b++) 9             for(c=0;c<=1;c++)10               for(d=0;d<=1;d++)11                 for(e=0;e<=1;e++)12                   System.out.printf("%d%d%d%d%d\n",a,b,c,d,e);13     }14 }

解法二:
位元1-5位的權值分別為:1、2、4、8、16,如果對應的十進位數位X,則
X=a*1+b*2+c*4+d*8+e*16,其中,a、b、c、d、e要麼是0要麼是1。
觀察運算式可以知道,(int)(X/16)即為高位的二進位位,(X%16)再除以相應的權即可得到次高位的二進位位、、、
 1 class test 2 { 3     public static void main(String [] args) 4     { 5         int[] Arr={1,2,4,8,16};//權值 6         int[] Res=new int[5]; 7         int num,Tem,Sh; 8         int j,k,i; 9         10         for(k=0;k<5;k++)  //清零11            Res[k]=0;12         for(j=0;j<32;j++)13         {   14             num=j;15             for(i=4;i>=0;i--)16             {17                 Tem=Arr[i];          //取權值待用18                 Sh=(int)(num/Tem);  //num<32,Sh要麼是0要麼是119                 Res[i]=Sh;20                 num=num-Tem*Sh;     21             }22 23             for(k=4;k>=0;k--)     //列印數組24               System.out.print(Res[k]);25             System.out.println();26         }27     }28 }

  

解法三:

類似解法二,(X%2)即為最低位的二進位位,(int)(X/2))再除以2可得次低位的二進位位、、、

 1 class test  2 { 3     public static void main(String[] args)  4     { 5         int[] res=new int[5]; 6         int i,j,h; 7         int temp; 8  9         for(i=0;i<5;i++)10             res[i]=0;11         for(i=0,j=0;j<32;j++,i=0)12         {13            res[i]=j;          //先把需要列印的位元賦給最低位14            while(res[i]>=2)  //當前位大於2,需要調整15            {16               temp=res[i]/2;     //餘數即為最低位,而商17               res[i]=res[i]%2;  //再賦給高一位18               res[++i]=temp;19            }20 21            for(h=4;h>=0;h--)  //列印數組22              System.out.printf("%d",res[h]);23            System.out.printf("\n");24         }25         26     }27 }

 

解法四:

思維類似十進位一樣,從00000開始,然後逐漸從低位加1,某一位滿2,則需要相應的調整。但調整時,總是從最低位開始,即只有當最低位滿2時,其他位才可能需要調整。

 1 class test { 3     public static void main(String[] args)  4     { 5         int i,j; 6         int[]arr=new int[5]; 7  8         for(i=0;i<5;i++) 9             arr[i]=0;10         System.out.printf("00000\n");  //先輸出00000的情況11 12         for(i=0,j=1;j<32;j++,i=0)13         {14            arr[i]=arr[i]+1;    //個位元加115            while(arr[i]==2)   //滿2,當前位清零,並向高位進位,迴圈判斷其它高位16            {17               arr[i++]=0;    18               arr[i]++;19            }20 21            for(i=4;i>=0;i--)22                System.out.printf("%d",arr[i]);23            System.out.printf("\n");24 25         }26     }27 }                                                                                                                                                                      

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.