简体   繁体   English

反转整数位的位置?

[英]reverse the position of integer digits?

i have to reverse the position of integer like this 我必须像这样反转整数的位置

input = 12345 输入= 12345

output = 54321 输出= 54321

i made this but it gives wrong output eg 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;
}

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;

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. 再获得一次迭代,你的代码就可以了。

If you try it once as an example, you'll see your error. 如果您尝试一次作为示例,您将看到您的错误。

Input : 12 输入 :12

first loop: 第一循环:

out : 12%10 = 2 / 1 = 2 out :12%10 = 2/1 = 2
i = 100 我= 100
test : 12/100 = 0 (as an integer) 测试 :12/100 = 0(整数)

aborts one too early. 过早地中止一个。

One solution could be testing 一种解决方案可能是测试

(num % i) != num (num%i)!= num

Just as one of many solutions. 就像众多解决方案中的一个。

This is a coding assignment for my college course. 这是我大学课程的编码任务。 This assignment comes just after a discussion on Operator Overloading in C++. 在对C ++中的运算符重载进行讨论之后,才进行此分配。 Although it doesn't make it clear if Overloading should be used for the assignment or not. 虽然不清楚是否应该使用重载进行分配。

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;
}
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;
}

} }

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 ...). 这将反转任何有符号或无符号整数(short,byte,int,long ...)中的位。 You can provide additional parameter nBits to frame the bits while reversing. 您可以提供额外的参数nBits来在反转时对位进行帧化。

ie 7 in 8 bit = 00000111 -> 11100000 7 in 4 bit = 0111 -> 1110 即7位8位= 00000111 - > 11100000 7位4位= 0111 - > 1110

那么,请记住整数除法总是在C中向下舍入(或者它是否为零?)那么,如果num < 10i = 10 ,那么num / i会是什么?

replace your while statement 替换你的while语句

with

while (i<10*num)

If I were doing it, I'd (probably) start by creating the new value as an int , and then print out that value. 如果我这样做,我(可能)首先将新值创建为int ,然后打印出该值。 I think this should simplify the code a bit. 我认为这应该简化代码。 As pseudocode, it'd look something like: 作为伪代码,它看起来像:

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());
int _tmain(int argc, _TCHAR* argv[])
{
int x = 1234;
int out = 0;
while (x != 0)
{
    int Res = x % (10 );
    x /= 10;
    out *= 10;
    out +=  Res;
}
cout << out;


} 

I have done this simply but this is applicable upto 5 digit numbers but hope it helps 我这样做很简单,但这适用于最多5位数,但希望它有所帮助

 #include<iostream>
    using namespace std;
void main()
{ 
    int a,b,c,d,e,f,g,h,i,j;
    cin>>a;
    b=a%10;
    c=a/10;
    d=c%10;
    e=a/100;
    f=e%10;
    g=a/1000;
    h=g%10;
    i=a/10000;
    j=i%10;
    cout<<b<<d<<f<<h<<j;
}`

public class TestDS { 公共课TestDS {

public static void main(String[] args) {

    System.out.println(recursiveReverse(234));
           System.out.println(recursiveReverse(234 ,0));



}



public static int reverse(int number){

    int reversedNumber = 0;
    int temp = 0;


    while(number > 0){

        //use modulus operator to strip off the last digit
        temp = number%10;

        //create the reversed number
        reversedNumber = reversedNumber * 10 + temp;
        number = number/10;

    }



    return reversedNumber;

}



private static int reversenumber =0;
public static int recursiveReverse(int number){

    if(number <= 0){

        return reversenumber;
    }

    reversenumber = reversenumber*10+(number%10);
    number =number/10;

    return recursiveReverse(number);

}

public static int recursiveReverse(int number , int reversenumber){

    if(number <= 0){

        return reversenumber;
    }

    reversenumber = reversenumber*10+(number%10);
    number =number/10;

    return recursiveReverse(number,reversenumber);

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM