# Algorithm preliminary: digit inversion

Source: Internet
Author: User
Description:
Compile a reverse function and declare a function similar to unsigned reverse (unsigned );
Only one 32bit non-negative integer is required,
Returns a 32-bit integer, but returns a reversed number.

Input:
Pass by parameters. A 32bit value is not negative and the maximum percentile value is not 0.

Output:
Output by return value

Difficulty index: ☆

CodeImplementation 1:
Unsigned reverse (unsigned num)
{
Unsigned result = 0;
While (Num> 0)
{
Result = Result * 10 + num % 10;
Num = num/10;
}
Return result;
}
Comments: Now it's quite simple to think about, but it was not done directly at the beginning.

the following are two stupid ideas, which will be used as a souvenir
code implementation 2:
unsigned reverse (unsigned num)
{< br> unsigned * A = new unsigned [];
unsigned result = 0;
for (INT I = 0; num> 0; I ++)
{< br> A [I] = num % 10;
num = num/10;
result = Result * 10 + A [I];
}< br> return result;
}
comments: an array is added, and the memory is virtually increased.

code implementation 3:
unsigned reverse (unsigned num)
{< br> char * STR = new char [];
ITOA (Num, STR, 10);
int N, j = 0;
N = strlen (STR) + 1;
char * temp = new char [N];
for (INT I = n-2; I> = 0; I --)
{< br> temp [J ++] = STR [I];
}< br> temp [N] = '\ 0 ';
unsigned result = atoi (temp);
return result;
}
comment: I think of converting it into a string and then converting it back to a number, it's really funny, but I'm familiar with the ITOA function, but it's still a bit rewarding.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.