Poj 1131 Al fractions decimal conversion between arbitrary hexadecimal

Source: Internet
Author: User

// Poj 1131 Al fractions decimal conversion between any hexadecimal notation
// You must convert an octal decimal point to a decimal point,
// The number of decimal places after conversion is three times the number of decimal places in the first eight decimal places before conversion, and no meaningless zero at the end is output (that is, the post zero ).
// The method I used is to multiply 10 and then get an integer for 8 (now we assume that the decimal point of P is converted to N, and the integer of N is also used :),
// Every conversion of one digit must start from the second digit s [len-1] to the highest digit of the decimal place (that is, the first digit after the decimal point ),
// Calculate the product G = A [J] * n + k each time (where k is the carry of the next product), and the base input digit K = g/P,
// Store the product in S [J] = g % P. The final integer k is used as the conversion result string.

I'm actually disgusted with this kind of high precision, but I also listed a Java method .. It seems that Java should be easy to learn ..

# Include <iostream>
# Include <string>
# Include <cstdlib>
# Include <algorithm>
Using namespace STD;

Char S1 [20], S2 [50], S3 [20];

Int main ()
{
Int I, T, J, K, G, L, Len ;;
While (scanf ("% s", S1 )! = EOF)
{
L = strlen (S1 );
Strcpy (S3, S1 );
Len = 3*(L-2 );
T = 0;
S2 [0] = '0 ';
S2 [1] = '.';
J = 2;
While (T <Len)
{
K = 0;
T ++;
For (I = L-1; I> 1; I --) // start from the percentile bit and use the round value of 10 to 8.
{
G = (S1 [I]-'0') * 10 + K;
K = g/8;
S1 [I] = g % 8 + '0 ';
}
S2 [J] = K + '0 ';
J ++;
}
S2 [J] = '\ 0 ';
Printf ("% s [8] =", S3 );
J --;
While (s2 [J] = '0') // locate the last value not 0.
J --;
For (I = 0; I <= J; I ++) // remove the output from the backend 0.
Printf ("% C", S2 [I]);
Printf ("[10] \ n ");
}
System ("pause ");
Return 0;
}

// On the basis of the above, write a decimal point that converts the decimal point of P to the decimal point of N.Program(Decimal conversion between decimal places)
/*
# Include <iostream>
# Include <string>
# Include <cstdlib>
# Include <algorithm>
Using namespace STD;

String Ss = "0123456789 abcdef ";

string change (string S, int P, int N)
{< br> int I, T, K, G, L, POs, Len;
string STR;
L = S. size ();
Pos = S. find (". ");
Len = 3 * (l-pos-1); // converts the decimal point of P to the decimal point of N, and the decimal point after conversion is three times the first, but no post-Zero output
T = 0;
STR + = "0. ";
while (T {< br> K = 0;
T ++;
for (I = L-1; i> Pos; I --) // round P by n
{< br> G = (s [I]-'0') * n + K;
K = int (G/P);
S [I] = g % P + '0';
}< br> STR + = ss. substr (k, 1);
}< br> return STR;
}

Int main ()
{
String S1, S2;
Int P, N, I, j;
While (1)
{
Cin> S1;
Cin> P> N;
S2 = change (S1, P, N );
I = s2.size ()-1;
While (s2 [I] = '0 ')
I --;
Cout <S1 <"[" <p <"] = ";
For (j = 0; j <= I; j ++)
Cout <S2 [J];
Cout <"[" <n <"]" <Endl;

}
System ("pause ");
Return 0;
}*/

/*
Java
// Method used:
// 1. bigdecimal. Add (bigdecimal );
// 2. bigdecimal. Divide (bigdecimal );
View plain
Import java. Math. bigdecimal;
Import java. util. collections;
Public class main

{
Public static void main (string [] ARGs)

{
Cin = new partition (system. In );
While (CIN. hasnext ())

{
String STR = cin. Next ();
Bigdecimal fin = new bigdecimal (0 );
For (INT I = 2; I <Str. Length (); I ++)

{< br> bigdecimal sub = new bigdecimal (INT) Str. charat (I)-48);
bigdecimal mom = new bigdecimal (8);
fin = fin. add (sub. divide (mom. pow (I-1);
}< br> system. out. println (STR + "[8] =" + FIN + "[10]");
}< BR >}*/

Contact Us

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.