Nov 23, 2008 at 5:56pm UTC
Hello. I have a "long" tipe number. How can I interchange the biggest odd digit with the smallest odd digit? Example: 34921 becomes 34129
Nov 23, 2008 at 6:16pm UTC
You'd have to decompose the number using %10 or sprintf(), then find the max and min, swap them, and finally recompose the new number.
Nov 24, 2008 at 5:09pm UTC
Thanks for your answer helios, but i don't know how to recompose the number.
Nov 24, 2008 at 5:13pm UTC
I will give you a hint ;)
321%10=1
321/10=32 (we're using integers here)
Nov 24, 2008 at 5:36pm UTC
Until now I have this:
#include<iostream.h>
void main()
{
long n, cop
int i, max, min, man;
cout<<"n=";cin>>n;
for(i=9;i>=1;i-=2)
{
cop=n;
while(cop)
{
if(cop%10==i)
{
max=i;
i=0;
cop=0;
}
cop=cop/10;
}
}
for(i=1;i<=9;i+=2)
{
cop=n;
while(cop)
{
if(cop%10==i)
{
min=i;
i=10;
cop=0;
}
cop=cop/10;
}
}
man=max;
max=min;
min=max;
I decomposed the number, found the min and the max, swap them, but I don't know how to recompose the number as helios told me
Last edited on Nov 24, 2008 at 5:36pm UTC
Nov 24, 2008 at 5:37pm UTC
How to re compose?
Well, let's say you have the numbers 3, 9, 0, 5, 1.
0*10=0
0+3=3
3*10=30
30+9=39
39*10=390
390+0=390
390*10=3900
3900+5=3905
3905*10=39050
39050+1=39051
Last edited on Nov 24, 2008 at 9:23pm UTC
Nov 24, 2008 at 7:12pm UTC
I think the easiest way is to remember the amount of numbers. In the example of helios you would get:
3 E4+
9 E3+
0 E2+
5 E1+
1 E0
=39051