Join Google™Code jam-China programming challenge (2)

Source: Internet
Author: User

I wrote the second question, with a total score of 500 and a score of 499.76. It is estimated that this score is based on time. The longer the time, the less the score. This time, I first compiled the copy directly.
Question:
Problem Statement

A square matrix is a grid of nxn numbers. For example, the following is a 3x3 matrix:
4 3 5
2 4 5
0 1 9
One way to represent a matrix of numbers, each of which is between 0 and 9 random Sive, is as a row-major string. to generate the string, simply concatenate all of the elements from the first row followed by the second row and so on, without any spaces. for example, the above matrix wocould be represented as "435245019 ". you will be given a square matrix as a row-major string. your task is to convert it into a string [], where each element represents one row of the original matrix. element I of the string [] represents row I of the matrix. you shoshould not include any spaces in your return. hence, for the above string, you wocould return {"435", "245", "019 "}. if the input does not represent a square matrix because the number of characters is not a perfect square, return an empty string [], {}.
Definition

Class:
Matrixtool
Method:
Convert
Parameters:
String
Returns:
String []
Method signature:
String [] convert (string S)
(Be sure your method is public)

Constraints
-
S will contain between 1 and 50 digits, inclusive.
Examples
0)


"435245019"
Returns: {"435", "245", "019 "}
The example above.
1)


"9"
Returns: {"9 "}

2)


"0123456789"
Returns :{}
This input has 10 digits, and 10 is not a perfect square.
3)


"3357002966366183191503444273807479559869883303524"
Returns: {"3357002", "9663661", "8319150", "3444273", "8074795", "5986988 "}

MyCode:

Public   Class Matrixtool
{
Public Matrixtool ()
{

}

Public   String [] Convert ( String S)
{
String [] Returnvalues;
// Square
If (S. Length > 50 )
{
Returnvalues =   New   String [ 1 ] {"This input Length Overflow."} ;
}
Else
{
Double D = System. Math. SQRT (( Double ) S. Length );
Double Di = System. Math. Ceiling (d );
If (D ! = DI)
{
Returnvalues =   New   String [ 1 ] {"This input has"+S. length. tostring ()+"Digits, and"+S. length. tostring ()+"Is not a perfect square."} ;
}
Else
{
Try
{
System. Convert. todouble (s );
}
Catch
{
Returnvalues =   New   String [ 1 ] {"This input hasn' t numbers."} ;
Return Returnvalues;
}
Int I = System. Convert. toint32 (DI );
Int K = 0 ;
// Conversion
Returnvalues =   New   String [I];
For ( Int J = 0 ; J < I; j ++ )
{
Returnvalues [J] = S. substring (K, I );
K = I * (J + 1 ) - 1 ;
}
}
}
Return Returnvalues;
}

}

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.