对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
样例输出
00000
00001
00010
00011
<以下部分省略>
#include
using namespace std;
int main()
{
int a[5] = { 0,0,0,0,0 };
int s = 4;
int temp;
for (int i = 0; i < 32; i++)
{
temp = i;
while (temp != 0)
{
a[s] = temp % 2;
temp = temp / 2;
s--;
}
for (int j = 0; j < 5; j++)
{
cout << a[j];
}
cout << endl;
}
return 0;
}
请问代码哪里错误了?可以编译但得不出正确答案
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- threenewbee 2020-04-07 17:21关注
问题解决的话,请点采纳
#include <iostream> using namespace std; int main() { int a[5] = { 0,0,0,0,0 }; int s = 4; int temp; for (int i = 0; i < 32; i++) { temp = i; s = 4; //加上 while (temp != 0) { a[s] = temp % 2; temp = temp / 2; s--; } for (int j = 0; j < 5; j++) { cout << a[j]; } cout << endl; } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 pyqt5tools安装失败
- ¥15 mmdetection
- ¥15 nginx代理报502的错误
- ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
- ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
- ¥100 描述一下元器件的基本功能,pcba板的基本原理
- ¥15 STM32无法向设备写入固件
- ¥15 使用ESP8266连接阿里云出现问题
- ¥15 BP神经网络控制倒立摆
- ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果