C++ 反转整数位的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4806167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reverse the position of integer digits?
提问by bbjkdsfj
i have to reverse the position of integer like this
我必须像这样反转整数的位置
input = 12345
输入 = 12345
output = 54321
输出 = 54321
i made this but it gives wrong output e.g 5432
我做了这个,但它给出了错误的输出,例如 5432
#include <iostream>
using namespace std;
int main(){
int num,i=10;
cin>>num;
do{
cout<< (num%i)/ (i/10);
i *=10;
}while(num/i!=0);
return 0;
}
回答by Elalfer
Here is a solution
这是一个解决方案
int num = 12345;
int new_num = 0;
while(num > 0)
{
new_num = new_num*10 + (num % 10);
num = num/10;
}
cout << new_num << endl;
回答by Doug T.
Your loop terminates too early. Change
您的循环过早终止。改变
}while(num/i!=0);
to
到
}while((num*10)/i!=0);
to get one more iteration, and your code will work.
再进行一次迭代,您的代码将起作用。
回答by StampedeXV
If you try it once as an example, you'll see your error.
如果您尝试一次作为示例,您将看到您的错误。
Input: 12
输入:12
first loop:
第一个循环:
out: 12%10 = 2 / 1 = 2
i = 100
test: 12/100 = 0 (as an integer)
输出:12%10 = 2 / 1 = 2
i = 100
测试:12/100 = 0(作为整数)
aborts one too early.
中止过早。
One solution could be testing
一种解决方案可能是测试
(num % i) != num
(num % i) != num
Just as one of many solutions.
就像许多解决方案之一。
回答by Amith Chinthaka
template <typename T>
T reverse(T n, size_t nBits = sizeof(T) * 8)
{
T reverse = 0;
auto mask = 1;
for (auto i = 0; i < nBits; ++i)
{
if (n & mask)
{
reverse |= (1 << (nBits - i - 1));
}
mask <<= 1;
}
return reverse;
}
This will reverse bits in any signed or unsigned integer (short, byte, int, long ...). You can provide additional parameter nBits to frame the bits while reversing.
这将反转任何有符号或无符号整数(short、byte、int、long ...)中的位。您可以提供附加参数 nBits 以在反转时对位进行帧处理。
i. e. 7 in 8 bit = 00000111 -> 11100000 7 in 4 bit = 0111 -> 1110
即 7 合 8 位 = 00000111 -> 11100000 7 合 4 位 = 0111 -> 1110
回答by StephanM
This is a coding assignment for my college course. This assignment comes just after a discussion on Operator Overloading in C++. Although it doesn't make it clear if Overloading should be used for the assignment or not.
这是我大学课程的编码作业。这个分配是在讨论 C++ 中的运算符重载之后进行的。尽管并不清楚是否应该将重载用于分配。
回答by Yiannis Hadjipolycarpou
The following code works for a two-digit number only.
以下代码仅适用于两位数。
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
cout << (n%10) << (n/10);
return 0;
}
回答by Saad Mirza
int a,b,c,d=0;
cout<<"plz enter the number"<<endl;
cin>>a;
b=a;
do
{
c=a%10;
d=(d*10)+c;
a=a/10;
}
while(a!=0);
cout<<"The reverse of the number"<<d<<endl;
if(b==d)
{
cout<<"The entered number is palindom"<<endl;
}
else
{
cout<<"The entered number is not palindom"<<endl;
}
}
}
回答by Jonathan Grynspan
Well, remember that integer division always rounds down (or is it toward zero?) in C. So what would num / i
be if num < 10
and i = 10
?
好吧,记住整数除法在 C 中总是向下舍入(或者向零?)。那么num / i
如果num < 10
和会是什么i = 10
?
回答by Hans
replace your while statement
替换你的 while 语句
with
和
while (i<10*num)
回答by Jerry Coffin
If I were doing it, I'd (probably) start by creating the new value as an int
, and then print out that value. I think this should simplify the code a bit. As pseudocode, it'd look something like:
如果我这样做,我(可能)首先将新值创建为int
,然后打印出该值。我认为这应该稍微简化代码。作为伪代码,它看起来像:
output = 0;
while (input !=0)
output *= 10
output += input % 10
input /= 10
}
print output
The other obvious possibility would be to convert to a string first, then print the string out in reverse:
另一个明显的可能性是先转换为字符串,然后反向打印字符串:
std::stringstream buffer;
buffer << input;
cout << std::string(buffer.str().rbegin(), buffer.str().rend());