Google China Code jam round2 div1 1000 question

Source: Internet
Author: User
Tags strtok

I did not open the question until the end to see what the 1000 question is.
Google also played a psychological game.
The simplest fraction is used to represent the equivalent repeating decimal places. The denominator must be a positive integer.
Enter a string of 3 to 10 characters (inclusive): A. B (c) to indicate a circular decimal number.
Enter the following conditions to ensure:
The character is only 0123456789 characters .()
A Indicates the integer part. A maximum of 1 digits (2nd characters are always '.')
B Indicates the non-repeating part, which can be at least 0 bits
C Indicates the cyclic part, which can be at least 0 bits.
Parentheses must appear in pairs or not.
Returns the score of a/B.

Test data:
"0. (3 )"
Returns: "1/3"
"1.3125"
Returns: "21/16"
"2.85 (23 )"
Returns: "14119/4950"
9.123 (456 )"
Returns: "3038111/333000"
0.111 (1 )"
Returns: "1/9"
"3. (000 )"
Returns: "3/1"

Below is what I wrote laterCode.

# Include < String >
# Include < Cstdio >
Using   Namespace STD;
Class Recurringnumbers
{
Public :
Int Gcd ( Int A, Int B)
{
Int T;
If ( < B) T = A, = B, B = T;
While (B)
{
T=A%B;
A=B;
B=T;
}
Return A;
}
String Converttofraction ( Const   String   & Decimalnumber)
{
Char Buf [ 32 ], [ 10 ], B [ 10 ];
Int Lena;
Strcpy (BUF, decimalnumber. c_str ());
Strcpy (A, strtok (BUF, " .() " ));
Char   * N = Strtok (null, " .() " );
Char   * N2 = Strtok (null, " .() " );
If (N2 ! = Null)
{
Strcat (A, N );
Strcpy (B, N2 );
Lena=Strlen (N );
}
Else
{
If (Strchr (decimalnumber. c_str (), ' ( ' ))
{
Lena=0;
Strcpy (B, n );
}
Else
{
Lena=Strlen (N );
Strcat (A, N );
Strcpy (B,"0");
}
}
Int Na = Atoi ();
Int NB = Atoi (B );
Int Nd = 1 ;
Int I, Ng, NT;
For (NT = 1 , I = 0 ; I < Lena; I ++ )
NT = NT * 10 ;
If (NB ! =   0 )
{
For (Nd = I = 0 ; B [I]; I ++ )
Nd = Nd * 10 + 9 ;
Na = Na * Nd + NB;
NT * = Nd;
}
NG = Gcd (Na, NT );
Na /= Ng;
NT /= Ng;
Sprintf (BUF, " % D/% d " , Na, NT );
Return   String (BUF );
}
} ;
# If 0
Int Main ()
{
Char   * S [] =
{
" 0. (0) " , // Returns: "1/3"
" 1.3125 " , // Returns: "21/16"
" 2.85 (23) " , // Returns: "14119/4950"
" 9.123 (456) " , // Returns: "3038111/333000"
" 0.111 (1) " , // Returns: "1/9"
" 3. (000) " , // Returns: "3/1"
0 ,
} ;
Recurringnumbers;
For ( Int I = 0 ; S [I]; I ++ )
Printf ( " % S \ n " , A. converttofraction ( String (S [I]). c_str ());

Return   0 ;
}
# Endif

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.