Deformation of the Yang Hui triangle

Source: Internet
Author: User

Question details 1 1 1 1 1 1 2 2 2 11 3 6 7 6 3 3 1 above the number of triangles, the first row has only one number 1, each of the following rows, is exactly the sum of the above number, the upper left number and the upper right number (if there is no number, it is regarded as 0 ). Returns the position where the first even number of row n appears. If there is no even number,-1 is output. For example, input 3 outputs 2, and input 4 outputs 3. Input n (n <= 1000000000) function header: C/C ++: int run (int n); java: public solution {public static int run (int n );} first, after reading this question, the first thing I feel is to store the transformed Yang Hui triangle into a two-dimensional array or recursively traverse it, And then traverse the first even number in the last row. Here through simple analysis, 1. the first two rows clearly show that there are no even numbers and 2. the first row has only one number, and each row has two more numbers than the first row. The Rows x have 2 x-1 Numbers and 3. the deformed Yang Hui triangle is symmetric and the numbers in the center are odd and 4. the rule introduced in this topic is: the first row has only one number 1, and each number in each of the following rows is exactly the number above it, the sum of three numbers, such as the upper left and upper right (if there is no such number, it is regarded as 0 ). After analysis, I thought that if there is a two-dimensional array, it should look like the following: 0 0 0 1 0 0 0 0 1 1 1 0 0 1 2 2 1 0 1 3 6 7 6 3 1... As a result, the code for first-thought algorithms and submitting them to the pongo Pang go.com hero club is as follows,

Public class Test {public static int run (int x) {if (x> 2) {int row = 2 * X-1; int [] [] t = new int [x] [row]; for (int j = 0; j <x; j ++) {// returns a value for the traversal row (int I = 0; I <row; I ++) {// returns a value for the traversal column if (j = 0) {// assign a value to row 0th if (I = x-1) {t [j] [I] = 1;} else {t [j] [I] = 0 ;}} else {if (I = 0) {// The first column t [j] [I] = t [J-1] [I] + t [J-1] [I + 1];} else if (I = row-1) {// The last column t [j] [I] = t [J-1] [I-1] + t [J-1] [I];} else {t [j] [I] = t [J-1] [I-1] + t [J-1] [I] + t [j- 1] [I + 1] ;}}// -------- }}for (int I = 0; I <row; I ++) {// traverse the first even if (t [x-1] [I] % 2 = 0) {return I + 1 ;}} return-1 ;} // start prompt: the start unique identifier of the Automatic Marking. Do not delete or add it. Public static void main (String args []) {System. out. println (run (0);} // end // prompt: the unique identifier of the Automatic Marking end. Do not delete or add it.}

 

Exception in thread "main" java. lang. outOfMemoryError: java heap space exception, memory overflow, occasional removal, the idea is correct, why is it wrong? Then I instantly think of something, as if there is an upper limit, right, that is "Input n (n <= 1000000000) ", that pitfall, there is such an upper limit, not bad, this idea is correct, but the wrong place is used. The question requirement is to find the position where the first even number of Line x appears. then, we have to change our mindset. So in order to better find its rule, we have to write a few more lines for analysis. The analysis results are as follows: 1. if there are no even numbers in the first two rows,-1 and 2 can be returned directly. the positions of the first even number of odd rows starting from the third row are 2 and 3. starting from the fourth row, the first even number of rows appears at least 3, and 4. based on the analysis made for the first time above, it is concluded that each row is symmetric, column x in row x is an odd number, and each number in each row is exactly the number above it, the sum of the three numbers in the upper left and upper right, after the final analysis and summarization, the algorithm code for returning the even position is as follows,
Public static int run (int x) {if (x <= 2) {// the first two rows directly return-1 return-1 ;} else if (x % 2 = 1) {// The first return 2;} return x/2% 2 + 3;} of an odd number row appears ;}

 

In a sense, there is nothing wrong with the idea of the first algorithm. It is searched through the most direct Traversal method. It only deals with the infinite traversal Problem of the number, leading to memory overflow. the second method is to find the rule by feeling, and then sum up. Although the second algorithm looks very concise and perfect to fulfill the requirements of the question, no matter the first wrong idea, second, they all gave me a process of thinking,

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.