如何反转程序的输出?
我必须将 43.62 这样的十进制数转换为二进制数。所以我首先编写了一个将 43 转换为二进制的基本程序。但我注意到我的程序以相反的方式打印出二进制数,因此它打印 1 1 0 1 0 1 而不是 1 0 1 0 1 1。我该如何解决这个问题。
我的代码:
#include <iostream>
using namespace std;
int main()
{
int number;
int remainder;
cout << "Enter a integer: ";
cin >> number;
while(number != 0)
{
remainder = number % 2;
cout << remainder << " ";
number /= 2;
}
int pause;
cin >> pause;
return 0;
}
I have to convert decimal numbers like 43.62 to binary. So i first wrote a basic program that converts 43 into binary. But I notice that my program prints out the binary number in reverse, so it prints 1 1 0 1 0 1 instead of 1 0 1 0 1 1. how can I fix this.
My Code:
#include <iostream>
using namespace std;
int main()
{
int number;
int remainder;
cout << "Enter a integer: ";
cin >> number;
while(number != 0)
{
remainder = number % 2;
cout << remainder << " ";
number /= 2;
}
int pause;
cin >> pause;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不要将每个数字发送到 cout,而是将它们发送到数组。然后以相反的顺序读出数组。或者将它们推入堆栈,然后将它们从堆栈中弹出。或者...
Instead of sending each digit to cout, send them to an array. Then read the array out in reverse order. Or push them onto a stack, and then pop them back off the stack. Or...
有点像用大锤来破解坚果,但这里有一个基于递归方法的解决方案:
只需将
cout
移动一行即可获得与之前相同的输出!Something of a sledgehammer to crack a nut, but here's a solution based on a recursive approach:
You can get the same output as you had before by simply moving the
cout
one line up!查看
vector
并思考保存余数而不是立即打印它们有何用处。请注意,您不必将内容放在向量的末尾。
vector::insert
让您指定一个位置...这有帮助吗?或者,您创建的算法从最低有效数字开始。有没有办法从最高有效数字开始?如果我有数字 42 (
0101010
),则最高有效数字代表 32,前面的 0 代表 64。如果我用 42 减去 32 会发生什么?Look at
vector
and think about how it could be useful to save the remainders instead of printing them right away.Notice that you don't have to put things at the end of the vector.
vector::insert
lets you specify a position... could that be helpful?Alternatively, the algorithm you created starts at the least significant digit. Is there a way to start from the most significant digit instead? If I have the number 42 (
0101010
), the most significant digit represents the 32s, and the 0 ahead of it represents the 64s. What happens if I subtract 32 from 42?存储结果然后向后打印会更容易。使用递归也是实现这一目标的另一种可能性。
It would be easier to store the results and then print them backwards. Using recursion is also another possibility to do just that.
最重要的位在前:
要先打印最低的有效位,请更改
for
循环的方向。Most significant bit first:
To print least significant bit first, change the direction of the
for
loop.在 C++ 中,您还可以使用位集容器来执行此操作,
In C++, you can also use a bitset container to do this,
只需使用字符串函数
Just use string functions
当您通过保留余数进行此类转换时,结果将始终被恢复。按照建议使用
bitwise &
:此示例将开始遍历数字的所有 8 位,检查该位是打开还是关闭,并相应地打印出来。
When you do such conversion by holding on to the remainder, the result will always be reverted. As suggested use
bitwise &
:This example will start going through all your 8 bits of the number and check if the bit is on or off and print that accordingly.
最佳选择 - 使用 C++ stringstream 格式化 I/O
Best option - Use C++ stringstream for formatting I/O