如何使写入输出方向相反 (C++)
How to make writing output in opposite direction (C++)?
我创建了一个简单的十进制到二进制程序。假设我输入数字 8。它写回 0001我希望它是 1000
我该怎么做?
代码在这里:
using namespace std;
int translating(int x);
int main()
{
int x;
int translate;
cout << "Write a number: ";
cin >> x;
cout << endl;
translate = translating(x);
cout << endl;
cout << endl;
return 0;
}
int translating(int x)
{
if (x == 1)
{
cout << "1";
return 0;
}
if ((x % 2)==1)
{
cout << "1";
return (translating((x-1)/2));
}
else
{
cout << "0";
return (translating(x/2));
}
}
不要直接写入输出,而是先将其写入临时字符串,然后从最后一个字符遍历该字符串到第一个字符。
很简单,很简单,你会踢自己。只需颠倒输出语句和递归函数调用的顺序即可。还修复了一个错误。
void translating(int x)
{
if (x < 2)
{
cout << x;
return;
}
if ((x % 2)==1)
{
translating((x-1)/2);
cout << "1";
}
else
{
translating(x/2);
cout << "0";
}
}
相关文章:
- 使用Unreal C++获取VR耳机的世界位置/方向
- 在C++中向零方向近似的最佳方法
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- OpenVR:向视图方向移动
- 来自逆 vp 矩阵和相机位置的光线方向不正确
- 带有枚举方向/类型的气泡排序结构数组
- OpenGL通常"Up"方向?
- 定义/预设NURBS起点和终点的方向
- 如何按另一个向量的方向调整一个向量?
- 粒子系统:所有粒子都朝同一方向运动
- C++指标评价方向
- SFML - 弹丸以不同的方向减速
- C++位移位在相反方向上具有相同的常量,结果不同,代码更改很小
- 如何在相对于观看方向的地形中移动
- RCPP 函数,用于在 4 个方向上生成随机游走
- "in"和"out"在参数方向上是什么意思?
- 使用libgpiod,如何动态切换请求行的方向
- OpenGL:在那个方向上旋转和移动
- Lambda 函数在特征中按元素方向
- 给定方向和位置如何使用 GLM 计算旋转矩阵