I need help with a java assignment. Basically I have to inverse a number without converting it from int. It must remain an int. I've been able to do that with the forllowing ugly code:
while(num > 0)
{
if (num / 10 < 0)
return(num);
else
{
rem = num % 10;
sum = sum * 10 + rem;
num = num / 10;
}
}
return(sum);
And whatever other code I have for intializing variables and inputing the number. The problem is, I need to do this using a recursive method(as in the method calls itself). I can't fiugure out how to do this. Any help?
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
See how the function calls itself, and decrements the value into its next iteration. Both do the same thing, just rewritten differently. I hope this gives you insight in the recursive code you are trying to write.
int numrev(int num)
{
if (num/10)
{
int msd; //Most signifigtant digit
double msd_power, smsd_power; //Power of the most signifigant and second most signifigant digits
msd_power = Math.floor(Math.log10((double)num));
msd = num/(int)Math.pow(10.0, msd_power);
num -= msd*(int)Math.pow(10.0, msd_power);
smsd_power = Math.floor(Math.log10((double)num));
return(numrev(num)*(int)Math.pow(10.0, msd_power-smsd_power) + msd);
}
return(num);
}
public static int inverseNum(int num, int sum, int rem)
{
if (num / 10 < 1)
{
if (rem == 0 || sum == 0)
return(num);
else
return(num + (sum * 10));
}
else
{
rem = num % 10;
sum = sum * 10 + rem;
num = Math.round(num / 10);
sum = inverseNum(num, sum, rem);
}
return(sum);
}
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
Starman Ghost:
Yours fails on negative numbers, and the Math.round() in this is also pointless. You should also be using default values for the extra parameters.
Last edited by Nach on Wed May 02, 2007 4:50 pm, edited 1 time in total.
May 9 2007 - NSRT 3.4, now with lots of hashing and even more accurate information! Go download it.
_____________ Insane Coding
unsigned int revvup(unsigned int in, unsigned int out)
{
return ((in) ? (revvup(in/10, (in%10)+out*10)) : out);
}
Don't need no steekeeng pows to do integer crap !
*cough*
This shameless plug sponsored by bat-a-cat, inc.
yeah, really.
The problem though is that the people grading this will be like, "wtf, hax".
Yeah really. It's pretty sweet, but kinda confusing.
Yeah, I didn't need the round function, so I removed it. I thought I was going to ned it but didn't. I also made it do negative numbers too. Was just a matter of and extra condition using my code.
public static int inverseNum(int num, int sum, int rem)
{
if (num / 10 < 1 && num /10 > -1)
{
if (rem == 0 || sum == 0)
return(num);
else
return(num + (sum * 10));
}
else
{
rem = num % 10;
sum = sum * 10 + rem;
num = num / 10;
sum = inverseNum(num, sum, rem);
}
return(sum);
}
Not as clean as the code of others, but it works. If I make it too complex like grins they'll think I cheated or something. Anyways, yep.
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
funkyass wrote:just to make sure, the assignment is to print the number backwards, not express it as denominator.
Yes, that is correct. Sorry if I wasn't clear. I noticed now that inverse can mean x inverse = 1/x. Oopsie.
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
funkyass wrote:just to make sure, the assignment is to print the number backwards, not express it as denominator.
Yes, that is correct. Sorry if I wasn't clear. I noticed now that inverse can mean x inverse = 1/x. Oopsie.
You meant you wanted to "invert" the digits.
Yes, that's what I meant to say. I misspoke.
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
Since you're dealing with integers, there is no fraction.
Haha. I amaze myself sometimes. I was so tired I forgot that int has no decimal places. I'll change that, thanks.
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]