对于长度为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 微信小程序协议怎么写
- ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
- ¥20 怎么用dlib库的算法识别小麦病虫害
- ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
- ¥15 java写代码遇到问题,求帮助
- ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
- ¥15 有了解d3和topogram.js库的吗?有偿请教
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看