Reverse of a number with leading zeroes

2020-03-26 05:37发布

How do we reverse a number with leading zeroes in number ? For ex: If input is 004, output should be 400.

I wrote below program but it works only when no leading zeroes in input.

int num;
cout<<"Enter number "<<endl;
cin>>num;

int rev = 0;
int reminder;
while(num != 0)
{
    reminder = num % 10;
    rev = rev * 10 + reminder;
    num = num / 10;
}
cout<<"Reverse = "<<rev<<endl;

Is there any way to input a number with leading zeroes ? Even then, Above logic doesn't work for such numbers.

Any simple solution ? It is doable by taking input as string and processing it. But that doesn't look nice.

*EDIT: If length of number is known, it looks to be possible to reverse a number with leading zeroes. (Without using string)*

I shall post the code as soon as it works.

EDIT 2: I tried to put back characters to cin stream and then read and calculate the reverse. It is working for 2 digit numbers.

But if length is known, its far easier to find reverse. All i need to do is, multiply by 10 for required number of times. So i think, i would go with string approach. Hoping that interviewer would be happy :)

标签: c++
10条回答
小情绪 Triste *
2楼-- · 2020-03-26 05:40

If you know the total width you'd like the number to be before-hand, you can reuse the code you have and store the results (from right to left) in a zero initialized array. Note: you'd probably want to add some error checking to the code listed below.

int num, width;

cout<<"Enter number "<<endl;
cin>>num;

cout<<"Enter width: "<<endl;
cin>>width;

int rev[width];
for (int i = 0; i < width; ++i)
    rev[i] = 0;

int cnt = width - 1;
int rev = 0;
int reminder;
while(num != 0)
{
    reminder = num % 10;
//    rev = rev * 10 + reminder;
    rev[cnt] = remainder;
    --cnt;
    num = num / 10;
}

cout << "Reverse: ";
for (int i = 0; i < width; ++i)
    cout << rev[i];
cout << endl;

This will allow you to manipulate the number more easily in the future as well.

查看更多
神经病院院长
3楼-- · 2020-03-26 05:41

Read the number in string format (that is, use std::string) and reverse the string.

查看更多
时光不老,我们不散
4楼-- · 2020-03-26 05:45

A recursive approach, but easily converted to a loop...

#include <iostream>

int f(int value = 1)
{
    char c;
    return (std::cin.get(c) && isdigit(c))
           ? (c - '0') * value + f(10 * value)
           : 0;
}


int main()
{
    std::cout << f() << '\n';
}
查看更多
相关推荐>>
5楼-- · 2020-03-26 05:45

Replace your while loop with a for loop with the same number of runs as you wish the original number has digits (including leading zeros). e.g. 004 would require the loop to be run 3 times, and not to terminate prematurely once x == 0.

查看更多
冷血范
6楼-- · 2020-03-26 05:47

Leading zeroes are not represented by binary numbers (int, double, etc.) So you'll probably have to use std::string. Read the input into the string, then call std::reverse() passing the string as input.

查看更多
▲ chillily
7楼-- · 2020-03-26 05:59

Yes, you must use a string. You cannot store leading zeros in an int.

查看更多
登录 后发表回答