|
From: Fabien V. <fv...@ne...> - 2001-04-21 20:19:33
|
Hi.
Palindrom numbers are numbers which you can "read" from the begining or the
end.
11
151
1551
15251
152251
and so on....
----- Original Message -----
From: "Bolek Vrany" <vr...@pa...>
To: <dev...@li...>
Sent: Saturday, April 21, 2001 2:50 PM
Subject: Re: [Dev-C++] Number backwards?
> Hi,
>
> what palindrom numbers are?
>
> > Hello,
> > To use a number backward first you need to separare the digits of
> > the no.
> >
> > To do this say you have nNo=5234
> > To separate thousands digit ie 5 thousnad_digit = nNo/1000;
> > To get the hundred digit ie 2 hunreds_digit =
> > (nNo%1000)/100;
> > ( In above nNo%1000 will give 234 and then 234/100 will
> > give 2 )
> > To get tens digit ie 3 tens_digit =
> > (nNo%100)/10;
> > ( In above nNo%100 will give 34 and then 34/10 will give 3 )
> > To get the units digit ie 4 unit_digit = (nNo%10);
> > ( In Above nNo%10 Will give 4 )
> >
> > % is Called MODULUS OPERATOR and it give remainder of
> > a division.
> >
> > Now you have separated all the digits of original no now to revert
> > the no. simply change the units digit to thousands digit,
> > tens digit to hundreds digit, hundreds digit to tens digit, thousands
> > digit to unit digit. The nos get reversed.
> >
> > RevNo = unit_digit*1000 + tens_digit*100 + hundreds_digit*10 +
> > thousand_digit ;
> >
> > ( This above expression is equal to RevNo = 4*1000 + 3*100
> > + 2*10 + 5 ; therefore RevNo=4325 )
>
> Sorry for talking about my function again. I am not sure what Jesper
> Ekberg needs to do - if he needs to convert 00012345 to 54321000 then
> the above is probably the best algorithm - provided you know the maximum
> number of digits. However, if he needs to convert 00012345 to just
> 54321, then he needs to find the number of digits first. That's why it
> is better to do it this way:
>
> unsigned backwards( unsigned n)
> // Reverse order of digits (in decadic)
> {
> unsigned result = 0;
>
> while (n > 0) {
> result = 10*result + n % 10;
> n = n / 10;
> }
> return result;
> }
>
> Sorry for not explaining it in previous mails. result is simply the
> result. 10*result shifts the result one digit left. n % 10 is the last
> digit of n and n = n/10 shifts n one digit right. So assuming n=12345 at
> the beginning it does the following:
> result=0, n=12345
> first iteration:
> result = 10*0 + 5 // == 5
> n = 1234;
> second it.:
> result = 10*5 + 4; // == 54
> n = 123;
> third it.:
> result = 10*54 + 3; // == 543
> n = 12;
> etc.
>
> This way he needs not know the number of digits.
>
> !!! WARNING: (I should have realized it immediately) There is a problem
> you will always have - all algorithms will lead to integer overflow in
> some cases. Assume max. unsigned is 65535 and you want to convert 12347.
> The correct result is 74321, which outside the range of unsigned. The
> correct version of my function is
>
> #include <stdexcept>
> #include <limits>
>
> unsigned backwards( unsigned n)
> // Reverse order of digits (in decadic)
> {
> unsigned result = 0, max = numeric_limits<unsigned>.max;
>
> while (n > 0) {
> if ((result > max / 10) ||
> // avoid overflow in multiplying 10*result
> (n % 10 > max - 10*result))
> // avoid overflow in adding n % 10
> throw overflow_error( "Overflow in function backwards");
> result = 10*result + n % 10;
> n = n / 10;
> }
> return result;
> }
>
> Or you must either restrict the range of n or the type of result must be
> at least one digit wider than the type of n. Might be double result and
> unsigned (long) n.
> --
> ============================================================
> Ing. Boleslav Vrany
> Stanice techniku DDM hl. m. Prahy
> Pod Juliskou 2a
> Praha 6 - Dejvice
> 16000
> Czech Republic
>
> tel. (420 2) 243 10 240
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|