用C++實現:01字串打印

問題描述

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

00000spa

00001.net

00010code

00011blog

00100string

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

輸入格式
本試題沒有輸入。
輸出格式
輸出32行,按從小到大的順序每行一個長度爲5的01串。
樣例輸出
00000
00001
00010
00011
<如下部分省略>
思路:看到01,很天然想到二進制,故此題化十進制爲二進制加法便可。
 1 #include<iostream>
 2 using namespace std;
 3 int main(void)
 4 {
 5     char* arr = new char[5];
 6     for (int i = 0; i < 5; i++)
 7     {
 8         arr[i] = '0';
 9     }
10     for (int i = 0; i < 32; i++)
11     {
12         cout << arr << endl;
13         arr[4] = arr[4] + 1;
14         for (int j = 4; j >= 0; j--)
15         {
16             if (arr[j] == '2')
17             {
18                 arr[j - 1] = arr[j - 1] + 1;
19                 arr[j] = '0';
20             }
21         }
22     }
23  delete[]arr;
24 return 0; 25 }

注意:第14行for裏面判斷語句j>=0,並不影響後面的arr[j-1]。由於i<32,也就是說,這個數在十進制下面最大是31,轉化成二進制就是11111,故無論怎麼加,arr[0]都不可能等於2,因此也就不會執行if語句。class

 

 

再分享幾個網上其餘的作法:
  1 //一:暴力(這個能夠有)
  2 
  3 #include <iostream>
  4 using namespace std;
  5 int main()
  6 {
  7     cout << "00000" << endl;
  8     cout << "00001" << endl;
  9     cout << "00010" << endl;
 10     cout << "00011" << endl;
 11     cout << "00100" << endl;
 12     cout << "00101" << endl;
 13     cout << "00110" << endl;
 14     cout << "00111" << endl;
 15     cout << "01000" << endl;
 16     cout << "01001" << endl;
 17     cout << "01010" << endl;
 18     cout << "01011" << endl;
 19     cout << "01100" << endl;
 20     cout << "01101" << endl;
 21     cout << "01110" << endl;
 22     cout << "01111" << endl;
 23     cout << "10000" << endl;
 24     cout << "10001" << endl;
 25     cout << "10010" << endl;
 26     cout << "10011" << endl;
 27     cout << "10100" << endl;
 28     cout << "10101" << endl;
 29     cout << "10110" << endl;
 30     cout << "10111" << endl;
 31     cout << "11000" << endl;
 32     cout << "11001" << endl;
 33     cout << "11010" << endl;
 34     cout << "11011" << endl;
 35     cout << "11100" << endl;
 36     cout << "11101" << endl;
 37     cout << "11110" << endl;
 38     cout << "11111" << endl;
 39     return 0;
 40 }
 41 
 42 // 方法二:五層循環法
 43 
 44 #include <iostream>
 45 using namespace std;
 46 int main()
 47 {
 48     int a, b, c, d, e;
 49     for (a = 0; a < 2; ++a)
 50         for (b = 0; b < 2; ++b)
 51             for (c = 0; c < 2; ++c)
 52                 for (d = 0; d < 2; ++d)
 53                     for (e = 0; e < 2; ++e)
 54                         cout << a << b << c << d << e << endl;
 55     return 0;
 56 }
 57 
 58 // 方法三:模擬二進制運算
 59 
 60 #include <iostream>
 61 #include <string>
 62 using namespace std;
 63 int main()
 64 {
 65     int i, j;
 66     string str = "00000";
 67     for (i = 0; i < 32; ++i)
 68     {
 69         cout << str << endl;
 70         str[4] += 1;
 71         for (j = 4; j >= 0; --j)
 72         {
 73             if (str[j] == '2')
 74             {
 75                 str[j - 1] += 1;
 76                 str[j] = '0';
 77             }
 78         }
 79     }
 80     return 0;
 81 }
 82 
 83 
 84 
 85 
 86 
 87 // 方法四:十進制轉換二進制法
 88 
 89 
 90 
 91 #include <iostream>
 92 using namespace std;
 93 int main()
 94 {
 95     for (int i = 0; i < 32; i++) {
 96         cout << i % 32 / 16 << i % 16 / 8 << i % 8 / 4 << i % 4 / 2 << i % 2 << endl;
 97     }
 98     return 0;
 99 }
100 
101 //五:
102 #include <iostream>
103 using namespace std;
104 int main() {
105     for (int i = 0; i <= 31; i++)
106     {
107         int a[5] = { 0 };
108         int num = i;
109         int z = 0;
110         while (num != 0)
111         {
112             a[z] = num % 2;
113             z++;
114             num /= 2;
115         }
116         for (int j = 4; j >= 0; j--)
117             cout << a[j];
118         cout << endl;
119     }
120     return 0;
121 }

原文連接:https://blog.csdn.net/u012110719/article/details/41870877stream

相關文章
相關標籤/搜索