用C++实现:01字串打印

问题描述

对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:

00000

00001

00010

00011

00100

请按从小到大的顺序输出这32种01串。

输入格式
本试题没有输入。
输出格式
输出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语句。

 

 

再分享几个网上其他的做法:
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/41870877


__EOF__

本文作者神楽桜KaguraSakura
本文链接https://www.cnblogs.com/KaguraSakura/p/12496385.html
关于博主:hello~好久不见,喜欢的话点个赞吧
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神楽桜KaguraSakura  阅读(1007)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示