【发布时间】:2021-11-30 05:03:34
【问题描述】:
#include <iostream>
using namespace std;
void binary(unsigned a) {
int i;
cout << "0" << endl;
do {
for (i = 1; i < a; i=i/2) {
if ((a & i) != 0) {
cout << "1" << endl;
}
else {
cout << "0" << endl;
}
}
}
while (1 <= a && a <= 10);
}
int main(void) {
binary(4);
cout << endl;
}
我写了一个关于二进制数的代码。 İt 应该尊重输入数字,例如 for
4 (0100) 为 2 (10)。但是我的代码无穷无尽,你能解释一下吗?我在视觉上写
studio,我不能使用
【问题讨论】:
-
我在visual studio写的,不能使用
因为visual studio中没有这样的库 一件非常好的事情。永远不要使用这个标题。相关:https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H. -
问问自己:如果
a是4,那么while (1 <= a && a <= 10)什么时候结束? -
@NathanOliver 它可以改变我刚刚写的。它应该起作用,我们的老师也可以给出另一个数字,但根据限制,它应该在 1 到 10 之间
-
因为 Visual Studio 中没有这样的库 -- Visual Studio 万岁。 -- 还有我们的老师 -- 你的老师给你
#include <bits/stdc++.h>?我希望不会。 -
你应该在互联网上搜索“c++ binary number cout”或类似的东西。 StackOverflow 和互联网上都有很多类似的问题。