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.